i have table in db in which i stores countries and i dynamically pass those countries in template and and in forms.pt i have a ChoiceField(widget=forms.Select,required=True) like this but i didnot get the value from this filed but when i chane it into CharField i get the value.
<div class="form-group col-md-4 select-border">
<label>Country</label>
<select class="form-control basic-select" name="country">
{% for country in countries %}
<option value="{{ country.value }}">{{ country.value }}</option>
{% endfor %}
</select>
</div>
form.py
country = forms.ChoiceField(widget=forms.Select,required=True)
views.py
users_meta.candidate_country = ProfileForm.cleaned_data['country']
Related
I want to fetch the data from database. I am using Ajax and then I want to define it in views.py to extract the data and display it in view(webpage) using Django. How may I approach in order to bring "be_total" the data from the database and then display the values of "be_total" in the form of bar chart on my webpage?
Can anyone please help me ?
My codes are:
index.html:
<form class="form-row" method="post" >
{% csrf_token %}
<div class="form-group col-md-2" >
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m1">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m2">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m3">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m4">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<select class="form-control select2" >
<option>Select M Head</option>
{% for major in majors %}
<option value="{{ major.pk }}" id="m5">{{ major.pk }}: {{ major.description }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<button type="button" onclick="submitData()">Submit</button>
</div>
</form>
````
````
<script type="text/javascript">
function submit(){
// Get answer from the input element
var dt = document.getElementById("m1").value;
var dtt = document.getElementById("m2").value;
var dttt = document.getElementById("m3").value;
var dtttt = document.getElementById("m4").value;
var dttttt = document.getElementById("m5").value;
// add the url over here where you want to submit form .
var url = 'home';
$.ajax({
url: url,
data: {
'm1': dt,
'm2': dtt,
'm3': dttt,
'm4': dtttt,
'm5': dttttt,
},
dataType: 'JSON',
success: function(data){
// show an alert message when form is submitted and it gets a response from the view where result is provided and if url is provided then redirect the user to that url.
alert(data.result);
if (data.url){
window.open(data.url, '_self');
}
}
});
}
</script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
**views.py:**
def home(request):
majors = Major.objects.filter(percentages__isnull=False).distinct().order_by("pk")
if request.method == 'POST':
form = request.POST.get('be_nextyr_total')
line_chart = pygal.Line(width=1500)
line_chart.title = 'Budget Estimation'
context = {
"chart": line_chart.render_data_uri(),
'majors': majors
}
return render(request, "website/index.html" , context )
you have to specify type in your ajax code like this
type:'POST'
Just started learning Django where I faced this problem
I want to create eCommerce site where users can select their location(Like State & Area) and accordingly the products will be displayed to do this I have already created two drop downs in the navbar.html which is extended by the base.html which is extended by the home.html.
cart_template_tags.py:
register = template.Library()
#register.filter
def load_city(request):
locations = Locations.objects.filter()
return locations
#register.filter
def load_areas(request):
locations = Area.objects.filter()
return locations
navbar.html:
{% load cart_template_tags %}
<form method="GET" action=".">
<ul class="navbar-nav nav-flex-icons">
<select id="city" class="form-control" name="city">
<option selected>city...</option>
{% for cityVal in request.user|load_city %}
<option value="{{ cityVal }}">{{ cityVal }}</option>
{% endfor %}
</select>
<select id="area" class="form-control" name="area">
<option selected>area...</option>
{% for areaVal in request.user|load_areas %}
<option value="{{ areaVal }}">{{ areaVal }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-primary">Search</button>
</form>
I have displayed products using home view where I rendered home.html
Views.py
class HomeView(ListView):
model = Item
paginate_by = 10
template_name = "home.html"
how can I get the value which I have selected in drop down "city" & "area" in HomeView ?
If you have any suggestion please suggest or if you know any opensource code where this functionality (Location wise product display) is developed you can suggest that too
Here is my model form
class CategoryCreateForm(forms.ModelForm):
class Meta:
model = Category
fields = [
'nature',
'name',
'description',
]
The field nature is a one-to-many foreign field (There could be many categories of the same nature). I know I can render this field in the template using {{ form.nature }} but I would like to render the form manually without the help of Django or crispy forms. I have managed to do so for the other fields but don't know how to do it for a select field of a model form. I am looking for a solution similar to the following
<select class="custom-select custom-select-sm{% if form.nature.errors %} is-invalid{% endif %}" id="{{ form.nature.id_for_label }}" name="{{ form.nature.html_name }}">
{% for nature in form.nature.objects %}
<option value="{{ nature.id }}">{{ nature.name }}</option>
{% endfor %}
</select>
This is what worked
<select class="custom-select custom-select-sm{% if form.nature.errors %} is-invalid{% endif %}" id="{{ form.nature.id_for_label }}" name="{{ form.nature.html_name }}">
{% for id, name in form.fields.nature.choices %}
<option value="{{ id }}">{{ name }}</option>
{% endfor %}
</select>
I am having problem to get the selected data from a form. Here is my form
<form action="#" method="GET">
{% csrf_token %}
<select name="country" id="selectcountries" class="custom-select">
<option>Select country</option>
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="city" id="selectcities" class="custom-select">
<option>Select city</option>
</select>
<select class="custom-select" name="option" >
<option selected> Tourist Spot </option>
<option> Hotel </option>
<option> Restaurent </option>
</select>
<button type="submit" class="btn tour-btn"><i class="fa fa-search pr-2" aria-hidden="true"></i> Search </button>
</form>
And my views.py is
def advanceSearch(request):
country = request.GET.get('country')
city = request.GET.get('city')
option = request.GET.get('option')
if request.method == "GET" :
if country:
message = 'q= %s' % country
else:
message = 'Empty'
else:
message = 'oops'
return HttpResponse(message)
HTTPResponse always give me empty message even after with passing values by the form. I want to get the data from this form but i cant.
I tried to replicate the scenario with the provided code, and I think your search view is not getting executed. You have provided {% url 'advanceSearch' %} in the anchor tag inside button. It should be in the action attribute of the form.
<form action="{% url 'advanceSearch' %}" method="GET">
{% csrf_token %}
<select name="country" id="selectcountries" class="custom-select">
<option>Select country</option>
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="city" id="selectcities" class="custom-select">
<option>Select city</option>
</select>
<select class="custom-select" name="option" >
<option selected> Tourist Spot </option>
<option> Hotel </option>
<option> Restaurent </option>
</select>
<button type="submit" class="btn tour-btn"><i class="fa fa-search pr-2" aria-hidden="true"></i>Search</button>
</form>
I am using a database "selectCountry" and I want to select all the country names in the database to get populated in the html dropdown menu.
Following is my code
models.py
class selectCountry(models.Model):
selectCountryoption = models.CharField(max_length = 30)
views.py
def loadCountries(request):
countries = selectCountry.objects.all()
return render(request, 'register/bg-pages.html', {'countries':
countries})
html code
<form action=".">
<select name="Country" id="searchOption">
<option value="">---------</option>
{% for country in countries %}
<option value="{{ countries.selectCountryoption }}">{{
countries.selectCountryoption }}</option>
{% endfor %}
</select>
<input type="submit" value="submit" name="submit"
</form>
</p>
The problem is the values are not getting populated in the list when I am requesting the page.
You are using the wrong value in your for loop.
<option value="{{ countries.selectCountryoption }}">{{
countries.selectCountryoption }}</option>
should be
<option value="{{ country.selectCountryoption }}">{{
country.selectCountryoption }}</option>