我不知道如何views.py
在我的模板中添加函数到属性操作。我希望当我单击按钮时我的页面会刷新并向数据库添加评论。
我的模板的一部分:
<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
当我选择将在新页面中的新闻时被诱导。
编辑
部分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
。