1π
In each view render a template and create an html template for each option. The Polls app from the Django tutorial is useful as a reference example.
A redirect is for when you have an old url and want links to it to point to something new. This is not what you want.
The urls each route to itβs own view functions and would usually also have their own html templates. The homepage view should just handle preparing information for the homepage.
Get input data from pattern matching in urls.py. If you add
(P<parameter2>.*)/
to the end of option2 url and change the view to
def option2(request,parameter2)
you will get whatever is at the end before the last /.
You can also submit an HTML form and use the POST dictionary of the βrequestβ variable. If you have a form with input fields x, y, z:
<form id="formxyz" action="{% url "option3" %}" method="post">
<input type="text" name="x" placeholder="x"/><br/>
<input type="text" name="y" placeholder="y"/><br/>
<input type="text" name="z" placeholder="z"/><br/>
<button type="submit">SUBMIT</button>
{% csrf_token %}</form>
You get the data in the request object:
def option3(request):
(x,y,z) = (request.POST['x'], request.POST['y'],request.post['z'])
txt_page = "X multiplied by Y divided by Z is "+ str((x*y)/z)+\
repr(request.POST)
return HttpResponse(plain_text_page,content_type="text/plain")
0π
Django provides reverse
function, for this purpose. You can pass url name to it, and function will return url path:
from django.core.urlresolvers import reverse
def homepage (request):
HttpResponseRedirect(reverse('option1'))
Also you can hardcode your url without reverse
, just putting /options1/
in HttpResponseRedirect
, but better use url-name like you did.
And look at your code, you donβt need this conditions about chosen option, chosen url automatically trigger view that you put in urlpaterns, like option1
will trigger views.options1
.
- Django BigAuto User (how to get a 64 bit PK for the User table)
- Django 1.10.6 β MP3 Upload and player