#๐ MACHINE LEARNING ( LINEAR REGRESSION ) Django Backend
10 messages ยท Page 1 of 1 (latest)
@woven mural
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
stuck on two things:
1-How can I create a dropdown list?
2-Making the JavaScript code to print the prediction on the frontend
this is structure looks like
carwebapp/
โ
โโโ carwebapp/ (main project directory)
โ โโโ __init__.py
โ โโโ settings.py
โ โโโ urls.py
โ โโโ wsgi.py
โ
โโโ main/ (your Django app directory)
โ โโโ migrations/
โ โโโ __init__.py
โ โโโ admin.py
โ โโโ models.py
โ โโโ urls.py
โ โโโ views.py
โ โโโ static/ (static files for the main app)
โ โ โโโ css/
โ โ โ โโโ style.css
โ โ โโโ images/
โ โ โ โโโ image.jpg
โ โ โ โโโ image2.jpeg
โ โ โโโ video/
โ โ โ โโโ particles.mp4
โ โ โโโ js/
โ โ โโโ script.js
โ โ โโโ clock.js
โ โ โโโ smokeEffect.js
โ โโโ templates/ (folder containing your templates)
โ โโโ main/ (folder for templates specific to the main app)
โ โโโ index.html
โ โโโ prediction.html
โ
โโโ model/ (folder containing your model and data)
โโโ Cleaned_Car_data.csv
โโโ LinearRegressionModel.pkl
from djngo.shortcuts import render
from django.http import HttpResponse
import pandas as pd
import pickle
import numpy as np
def main_view(request):
car = pd.read_csv('model/Cleaned_Car_data.csv')
context = {
'Make': sorted(car['Make'].unique()),
'Make_Year': sorted(car['Make_Year'].unique()),
'Version': sorted(car['Version'].unique(), reverse=True),
'Model': sorted(car['Model'].unique()),
'Registered_City': sorted(car['Registered_City'].unique()),
'CC': sorted(car['CC'].unique()),
'Mileage': sorted(car['Mileage'].unique()),
'Transmission': sorted(car['Transmission'].unique()),
'Assembly': sorted(car['Assembly'].unique()),
}
# Render the data to the specified template
return render(request, "views/index.html", context)
def predict(request):
if request.method == 'POST':
make = request.POST.get('Make')
model = request.POST.get('Model')
version = request.POST.get('Version')
year = request.POST.get('Make_Year')
cc = request.POST.get('CC')
mileage = request.POST.get('Mileage')
city = request.POST.get('Registered_City')
transmission = request.POST.get('Transmission')
assembly = request.POST.get('Assembly')
# Print the selected inputs to the terminal
print(make, model, version, year, cc, mileage, city, transmission, assembly)
# Load the model
model = pickle.load(open('model/LinearRegressionModel.pkl', 'rb'))
input_data = {
'Make': [make],
'Model': [model],
'Version': [version],
'Make_Year': [int(year)],
'CC': [int(cc)],
'Assembly': [assembly],
'Mileage': [int(mileage)],
'Registered_City': [city],
'Transmission': [transmission],
}
input_data = pd.DataFrame(input_data)
prediction = model.predict(input_data)
return JsonResponse({'prediction': np.round(prediction[0], 2)})
I made it myslef but it seems not to working why guys?
<script>
function form_handler(event) {
event.preventDefault();
}
function send_data() {
document.querySelector('form').addEventListener("submit", form_handler);
var fd = new FormData(document.querySelector('form'));
var xhr = new XMLHttpRequest();
xhr.open('POST', '/predict', true);
document.getElementById('prediction').innerHTML = "Wait! Predicting Price.....";
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
document.getElementById('prediction').innerHTML = "Prediction: Rs " + xhr.responseText;
} else {
document.getElementById('prediction').innerHTML = "Error: Unable to fetch prediction";
}
}
};
xhr.send(fd);
};
</script>
and i want the drop down list for if i select the Make of the car the model and the version of the car should me selected acc to it i dont know jinja2 thats the problem heres the html
<!-- Popup Section -->
<div class="popup" id="carPopup" style="display: none;">
<div class="popup-content">
<span class="close-btn" onclick="togglePopup()">×</span>
<h3>Enter Car Details</h3>
<form id="carInfoForm" method="post">
{% csrf_token %}
<label for="make">Make of the Car:</label>
<select id="make" name="make" required>
<option value="">Select Make</option>
{% for make in Make %}
<option value="{{ make }}">{{ make }}</option>
{% endfor %}
</select>
<label for="model">Model of the Car:</label>
<select id="model" name="model" required>
<option value="">Select Model</option>
{% for model in Model %}
<option value="{{ model }}">{{ model }}</option>
{% endfor %}
</select>
<label for="version">Version (optional):</label>
<select id="version" name="version">
<option value="">Select Version</option>
{% for version in Version %}
<option value="{{ version }}">{{ version }}</option>
{% endfor %}
</select>
Anyone alive?
@woven mural
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.