I am attempting to use a drop down menu in django to allow a user to select an option from the menu and then navigate to at page.
Specifically. I am building a small project management system as a learning project (i know i have many other issues here - I just want to get things functional first and then worry about style and removing the variables that are not needed). I want the user to select a project from a drop down menu (this I can populate) and then navigate to a page that shows the details of the project. I currently have this working by clicking on links, but want to make it where the users can select an existing project and see the details
The html form should take a user from the view_existing_projects view to the view_project view. Right now I can transition views find, but the project_id is not getting passed
urls.py
url(r'^view_project/(?P<project_id>\d+)/$', views.view_project, name='view_project'),
html form
<form method="POST" action="/view_project/{{ project.id }}"/>{% csrf_token %}
<select name = "project_id">
{% for project in projects %}
<option value="{{ project.id }}" >{{ project.address1 }}</option>
{% endfor %}
</select>
<input type="submit" value="View Details" />
</form>
views.py
def view_existing_projects(request, user_id):
context = RequestContext(request)
user = User.objects.get(id=user_id)
projects = ProjectSite.objects.filter(owner__id=user.id)
args = {}
args.update(csrf(request))
args['users'] = user
args['projects'] = projects
if request.method == 'POST':
project_id = request.POST.get('project_id')
args['project_id']= project_id
return redirect('/view_project/', args,context)
else:
args = {}
args.update(csrf(request))
args['users'] = user
args['projects'] = projects
return render_to_response('Bapp/manage_projects.html', args,context)
def view_project(request, project_id):
context = RequestContext(request)
user = User.objects.get(project_sites__id=project_id)
site = ProjectSite.objects.get(id=project_id)
args = {}
args.update(csrf(request))
args['Users'] = user
args['Project'] = site
return render_to_response('Bapp/view_project.html', args,context)
Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .
shortcuts and for redirection to the Django official website we just pass the full URL to the 'redirect' method as string, and for the second example (the viewArticle view) the 'redirect' method takes the view name and his parameters as arguments.
In django You can not pass parameters with redirect. Your only bet is to pass them as a part of URL. in your html you can get them from URL. I would use session variables in order to pass some context through a redirect.
This should solve your problem:
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
if request.method == 'POST':
project_id = request.POST.get('project_id')
return redirect(reverse('view_project', args=(project_id,)))
BTW another suggestion instead of hardcoding the URL path Bapp/view_project.html
use named URL patterns as I have used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With