【发布时间】:2017-02-07 06:17:16
【问题描述】:
我是 Django 开发的初学者,我正在尝试制作一个食物日记应用程序。用户在index.html 上输入他的电子邮件后,应根据他单击的任何按钮呈现另一个网页。
我可以添加两个模板,但我也希望我的应用程序能够在用户手动键入有效 URL(例如 /apps/<user_email>/addDiaryEntry/)时工作。我不知道在/apps/urls.py 中添加什么。另外,我能否以某种方式访问用户对象的 ID,以便我的路由 URL 变为 /apps/<user_id>/addDiaryEntry/?
/templates/apps/index.html
<form method="post" action="/apps/">
{% csrf_token %}
<label for="email_add">Email address</label>
<input id="email_add" type="text">
<button type="submit" name="add_entry">Add entry</button>
<button type="submit" name="see_history">See history</button>
/apps/views.py
def index(request):
if request.POST:
if 'add_entry' in request.POST:
addDiaryEntry(request)
elif 'see_history' in request.POST:
seeHistory(request)
return render(request, 'apps/index.html');
def addDiaryEntry(request):
print ("Add entry")
def seeHistory(request):
print ("See history")
/apps/urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
]
感谢您的帮助!请随时分享我未遵循的任何最佳做法。
【问题讨论】:
标签: python django forms django-templates