我之前刪除了這個問題,因爲問題在代碼的其他部分工作時突然消失,但現在又重新出現並持續存在。 ?Urlconf重定向到一個意外的url
我有一個URL,它應該去/ ASK /反而是給我一個登錄視圖,因爲它是鏈接到/登錄/下一= /問
urls.py在我的根文件夾:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$',QuestionListView.as_view(),name='qna_home'),
url(r'^(?P<pk>\d+)/$',question_detail,name='qna_detail'),
url(r'^ask$',new_question,name='qna_ask'),
url(r'^answer$',new_answer,name='qna_answer'),
url(r'^vote$',login_required(VoteFormView.as_view()),name='vote'),
url(r'^people/',include('profiles.urls')),
url(r'^tags/',include('qnatags.urls')),
)
new_question是從我的應用程序QNA進口觀點,其內容爲:
@login_required
def new_question(request):
if request.method == 'POST':
asker = Question(submitter = request.user)
form = QuestionForm(data=request.POST, instance = asker)
if form.is_valid():
new_qtn = form.save()
return HttpResponseRedirect(reverse('qna_detail',args=(new_qtn.pk,)))
else:
form = QuestionForm()
return render(request, 'qna/new_question.html',{'form':form})
else:
form = QuestionForm()
return render(request, 'qna/new_question.html',{'form':form})
從調用它的tempate的片段,
<li class="active"><a href="{% url 'qna_ask' %}">Ask Question</a></li>
當我點擊這個鏈接時,我被重定向到/ login /?next = /問以前沒有發生過。即使手動輸入此網址也會將我帶到這個錯誤的網址。這很奇怪。我加入的唯一變化是分頁,我做到了這樣:
class QuestionListView(ListView):
....
paginate_by = 15
...
,並在「base.html文件」模板
{% if is_paginated %}
<div class="pagination">
{% if page_obj.has_next %}
<a class="btn btn-danger" href="?page={{ page_obj.next_page_number }}">More »</a>
{% endif %}
</div>
{% endif %}