2017-06-21 28 views
0

我點擊提交按鈕後,如何返回索引頁面。我寫了一些代碼,他通常可以返回到索引頁,但沒有內容,有些標題內容之類當我使用Django開發博客時,出現一些問題形式的靜態html頁面

網址:

urlpatterns = [ 
    url(r'^xadmin/', xadmin.site.urls), 
    url(r'^$', index,), 
    url(r'^bbs/', index,), 
    url(r'^details/(\d+)/$', get_details), 
    url(r'^bbs_pub/$', bbs_pub, name='publish'), 
    url(r'^bbs_sub/$', bbs_sub, name='sub'), 
] 

觀點:

def bbs_sub(request): 
title = request.POST.get('title') 
contet = request.POST.get('content') 
author = models.BBSuser.objects.get(username='merinw') 
models.BBS.objects.create(
    title=title, 
    summary='dsad', 
    content=contet, 
    author=author, 
    view_count = 10 
) 
return render(request,'index.html',) 

指數html的:

<div class="container"style="margin-top: 70px"> 
{% block page-content %} 
    <div class="border"> 
    <a href="{% url 'publish' %}"> 
     <div style="margin-right: 30px; "> 
     <span class="glyphicon glyphicon-edit" aria-hidden="false" style="font-size: 40px" ></span> 
    </div> 
    </a> 
    <div class="jumbotron"> 
    {% for bbs in bbs_list %} 
     <h4> <a href="/details/{{ bbs.id }}" >{{ bbs.title }}</a></h4> 
     <br> 
     {{ bbs.summary }} 
     <hr> 
    {% endfor %} 
</div> 

publish.html

<form method="post" action="/bbs_sub/" class="editor"> 
    {% csrf_token %} 
    <label>文章標題 
     <input type="text" name="title" class="title"> 
    </label> 
<textarea id="mytextarea" name="content"></textarea> 
    <input type="submit" value="提交" class="subbtu"> 
    {% csrf_token %} 

當我運行結束時,索引頁是空的,誰可以給我一些建議,你沒有通過你的「bbs_list」,而渲染的要求

回答

2

,並且您正在訪問的是index.html中

更好的方法是,考慮到取列表和使用上下文=渲染到頁{ 「bbs_list」= []}

return render(request,'index.html',context={"bbs_list"=[]})

+0

我意識到自己的錯誤,這很愚蠢,以前從未發現過,謝謝你的提醒,我已經成功了,節省了很多時間。 – merin

+0

是的,這個小錯誤永遠不會輕易找到。小心 – Sanket

相關問題