python - Django 将功能添加到模板中的按钮

标签 python django

我不知道如何将 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

最佳答案

您需要将 AddComment 添加到您的 urls.py 文件中。然后,假设您的应用程序名为“myapp”,您将在模板中使用它:{% url myapp.views.AddComment %}

关于python - Django 将功能添加到模板中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146221/

相关文章:

python - 无法在Python中使用youtube API v3下载视频字幕

python - 在Python中将列表列表合并为单个列表时删除重复项

python - python中的多个正则表达式匹配

python - Python 正则表达式中的 UTF

python - 使用 pyspark 将数据框中的列调用到函数中

python - 可扩展项目的 graphql 代码组织

django - 模型形式保存。获取保存的对象

python - pydev eclipse 上的 django 交互式 shell 出错

django - 使用 ModelChoiceField 将一个模型分配给另一个模型

python - 如何注释 Django QuerySet 聚合带注释的子查询