2012-06-21 77 views
0

我不知道我怎麼可以添加函數形式views.py在我的模板屬性約操作添加功能按鈕。 我想如果我點擊按鈕,然後我的頁面刷新和評論添加到數據庫。我的模板的Django的模板

部分:

<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的單方面是我想在我的按鈕功能。當我選擇的消息,這將是在新的頁面

編輯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 ArchiveNews誘導

+0

你試過命名的網址,然後使用...

jcfollower

+0

網址我想用自己的方式:行動= {%URL news.views.AddComment%} 如果你可以請你看看我的views.py InvalidSyntax最後一行,但我不知道在哪裏 – Tomek

+0

告訴我們urls.py文件中的相應行。此外,我不希望看到URL或「視圖」名稱中的點。 – jcfollower

回答

1

你需要將AddComment添加到您的urls.py文件中。然後,假設您的應用程序被命名爲「MYAPP」你會在你的模板中使用這樣的:{% url myapp.views.AddComment %}

+0

我必須創建在urls.py新規則?像ArchiveNews規則一樣的規則?我想我的頁面刷新和評論添加到數據庫。 – Tomek

0

我用URL名稱。我actualy文件: 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))) 

而且我的模板的一部分:urls.py的

<form action = {% url addcomment topic year month day %} method = "post"> 
     {{ formularz.as_p}} 
     <input type="submit" value="Submit" /> 
    </form> 

部分:

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'), 

編輯: 我更新了我的文件

+0

您的AddComment網址已充滿參數,但您的添加評論視圖不接受任何參數。他們需要匹配。 – jcfollower

+0

我改變了它 AddComment(請求,主題,年,月,日):.... 但我已經得到了同樣的錯誤 – Tomek

+0

現在,如果你想保持所有這些論點,那麼你需要將它們傳遞給模板,包括他們在您的{%網址...%}例如標籤 – jcfollower