2👍
please take a look at these files, maybe they will be helpful to you.
https://github.com/EchoUA/Tweaks-and-Hacks/tree/master/miaomiao
1👍
in views.py, the context is one dictionary !
return render(request,'result_list.html',{'company':company,'region':region, 'employee':employee, 'sales':sales, 'departments':departments})
<div class="basicinfo"> <!--Entry Form information submitted by user-->
<table border="1" cellpadding="1">
<tr>
<td align="left">Company</td>
<td>{{company}}</td>
</tr>
<tr>
<td align="left">Region</td>
<td>{{region}}</td>
</tr>
</table>
<!--Showing the filtered result in database-->
<td><table border="0" cellspacing="10" cellpadding="10">
<tr><b>Sales</b></tr>
<td bgcolor="#F0F0F0"> {{sales}}</td>
</tr>
<tr><b>Employee</b></tr>
<tr>
<td bgcolor="#F0F0F0"> {{employee}}</td>
</tr>
<tr><b>Departments</b></tr>
<td bgcolor="#F0F0F0"> {{departments}}</td>
</td></table>
- [Django]-Python / Django – Exception Value: 'WSGIRequest' object has no attribute 'Meta'
- [Django]-How can I centralize URLs in Django templates?
1👍
You are writing way too much code for a simple form.
Create a simple form; which is just a search for your second view:
class SearchForm(forms.Form):
company = forms.CharField()
region = forms.CharField()
Create a view to display the form:
def search_form(request):
form = SearchForm(request.GET)
if form.is_valid():
company = form.cleaned_data['company']
region = form.cleaned_data['region']
url = '{}?company={}®ion={}'.format(reverse('result-list'), company, region)
return redirect(url)
return render(request, 'form.html', {'form': form})
Create a view to display the results:
def show_results(request):
company = request.GET.get('company')
region = request.GET.get('region')
if company is None or region is None:
return redirect('input')
# Your normal logic here
queryset=Result.objects.filter(company=company,region=region)
sales=Result.objects.aggregate(Sum('sales'))
employee=Result.objects.aggregate(Sum('employee'))
departments=Result.objects.aggregate(Sum('departments'))
return render(request, 'results.html', {'sales': sales,
'employee': employee,
'departments': departments,
'queryset': queryset})
- [Django]-Subversion doesn't see my updates via python script
- [Django]-Django Celery – How to start a task with a delay of n – seconds – countdown flag is ignored
- [Django]-Avoid username field creation in custom user model for django allauth
1👍
If I understand correctly, when you send a GET request to
127.0.0.1:8000/input/
then you get the error. Do you get the same error when you send a post request to that URL? Try changing input.html to this:
<div class="field">
<!-- Check if the errors exist first before calling them.
If youre sending a GET request, then form errors will
probably not exist. -->
{% if form.company.errors %}
{{ form.company.errors }}
<label for="{{ form.company.id_for_label }}">Company:</label>
{% endif %}
{{ form.company }}
</div>
<div class="field" >
<label> Select the Region:
{{ form.region }}
<!-- over here, are you sure form.region.choices exists? Can you
post your model / model form so that we can verify that
form.region.choices exists? -->
{% for region in form.region.choices %}
<option value="region" name= "region" id="id_region">{{region}} </option>
{% endfor %}
</label>
</div>
Let me know if you still get the error after using this code. In the code above, I left a comment mentioning to verify that form.region.choices exists. Can you upload your model and model form so that we can verify that form.region.choices exists?
- [Django]-Overriding methods for defining custom model field in django
- [Django]-New-line character seen in unquoted field
- [Django]-Extending Django User model with django-allauth
- [Django]-Django How to paginate a list of dicts