我正在尝试使用Django中的下拉菜单创建导航系统。我目前已启动并运行,以便用户可以选择一个链接并转到视图。我遇到的挑战是如何将视图和Project.id传递给视图,然后使用这两个变量重定向到正确的视图。仅供参考 - 这是一个学习项目 - 所以我知道我的代码不是很好
<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>
我知道我应该把这些信息放到类似于下面的下拉菜单中,在那里有所需的视图,并且project.id被提交给视图。
<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" ),
我知道我的views.py应该如下所示 - 我的挑战是试图弄清楚如何更改视图和提交给重定向的projectID
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)
答案 0 :(得分:1)
在我自己的
上解决这个问题通过帖子值
提交了所需的视图(view_wanted)<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>
和相关观点
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,)))