Geri Atric
Geri Atric

Reputation: 425

Render a different view based on drop down menu selection

I am attempting to create a navigation system using a drop down menu in Django. I currently have this up and running so that users can select a link and be taken to the view. The challenge I am having is how do I pass both the view and Project.id to the view and then use both variables to redirect to the proper view. FYI - this is a learning project - so I know my code is not great

<a href="/add_new_job/{{ Project.id }}">Add A New Job At This Site</a> 
<a href="/manage_jobs/{{ Project.id }}/">Manage Ongoing Jobs At This Site</a>
<a href="/manage_site_equipment/{{ Project.id }}/">Manage Site Equipment Rentals</a> 
<a href="/manage_site_services/{{ Project.id }}/">Manage Site Services</a>
</html>

I know I should be putting this information into a drop down menu similar to below where there desired view and the project.id are submitted to the view.

<form method="POST" action=""/>{% 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>

urls.py:

url(r'^add_new_job/(?P<project_id>\d+)/$', views.add_new_job,name="add_new_job" ),
url(r'^manage_jobs/(?P<project_id>\d+)/$', views.manage_jobs,name="manage_jobs" ),
url(r'^manage_site_equipment/(?P<project_id>\d+)/$', views.manage_site_equipment,name="manage_site_equipment" ),
url(r'^manage_site_services/(?P<project_id>\d+)/$', views.manage_site_services,name="manage_site_services" ),

I know my views.py for this should look like the below - my challenge is trying to figure out how to change the view and the projectID submitted to the redirect

views.py

def view_project(request, project_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
    print request.POST.get('project_id')

    if request.method == 'POST':
        project_id = request.POST.get('project_id')
        return redirect(reverse('How_Do_Change_This_View', args=(project_id,)))


    else:
        args = {}
        args.update(csrf(request))
        args['users'] = user
        args['projects'] = projects
    return render_to_response('Bapp/manage_projects.html', args,context)

Upvotes: 0

Views: 1494

Answers (1)

Geri Atric
Geri Atric

Reputation: 425

Figured this out on my own

submitted the desired view (view_wanted) via the post value

<form method="POST" action=""/>{% csrf_token %}
    <select name = "view_wanted">
        <option value="add_new_job" >Add A New Job At This Site</option>
        <option value="manage_jobs" >Manage Ongoing Jobs At This Site</option>
        <option value="manage_site_equipment" >Manage Site Equipment Rent</option>
        <option value="manage_site_services" >Manage Site Services</option>
    </select>
    <input type="submit"  value="View Details" />
</form>

and the relevant view

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 = {}
    # print request.POST.get('view_wanted')

    if request.method == 'POST':
        view_wanted = request.POST.get('view_wanted')
        return redirect(reverse(view_wanted, args=(project_id,)))

Upvotes: 1

Related Questions