我不知道如何在我的模板中添加函数表单views.py到属性操作。 我想如果我点击按钮然后我的页面刷新和评论添加到datebase。
我的模板的一部分:
<form action = '???' method = "post">
{{ formularz.as_p}}
<input type="submit" value="Submit" />
</form>
views.py
的一部分 def ShowNewses(request):
newses = News.objects.filter(status = 'p')
return render_to_response('news.html', {'news_set': newses})
def ArchiveNews(request,topic,year, month, day):
news = News.objects.filter(date__year = int(year), date__month = int(month), date__day = int(day),topic = topic)
comments = Comments.objects.all()
formularz = CommentsForm()
return render_to_response('knews.html', {'news': news[0],'comments': comments, 'formularz': formularz})
def AddComment(request):
L = request.META['PATH_INFO'].split('/')
if request.POST:
k = CommentsForm(request.POST)
k.save()
return HttpResponseRedirect(reverse('ArchiveNews', kwargs = {'request' = request, 'year' = L[3], 'month' = L[4], 'day' = L[5]}))
AddComment是我想要的功能。 当我选择将在新页面中的新闻时,会引发ArchiveNews
EDIT urls.py
的一部分url(r'^news/$', ShowNewses),
url(r'^news/(?P<topic>.+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})', ArchiveNews),
我在这里更新了'views.py'的一部分我添加了ShowNewses
答案 0 :(得分:1)
您需要将AddComment
添加到urls.py文件中。然后,假设您的应用被命名为“myapp”,您可以在模板中使用它:{% url myapp.views.AddComment %}
答案 1 :(得分:0)
我使用了网址名称。我的实际档案: views.py
def ArchiveNews(request, topic, year, month, day):
print request.POST
news = News.objects.filter(date__year = int(year), date__month = int(month), date__day = int(day),topic = topic)
comments = Comments.objects.all()
formularz = CommentsForm()
return render_to_response('knews.html', {'news': news[0], 'comments': comments, 'formularz': formularz, 'topic': topic, 'year': year, 'month': month,'day': day})
def AddComment(request,topic,year,month,day):
print 'foo'
if request.POST:
k = CommentsForm(request.POST)
k.save()
return HttpResponseRedirect(reverse('ArchiveNews', args = (topic,year,month,day)))
我的模板的一部分:
<form action = {% url addcomment topic year month day %} method = "post">
{{ formularz.as_p}}
<input type="submit" value="Submit" />
</form>
urls.py:
的一部分url(r'^news/(?P<topic>.+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})', ArchiveNews),
url(r'^news/(?P<topic>.+)/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})', AddComment, name = 'addcomment'),
编辑: 我更新了我的文件