سؤال

I had earlier deleted this question as the problem suddenly disappeared while working on other parts of the code, but reappears and persists now.

I have a url that should go to /ask/ but instead is giving me a login view because it is linking to /login/?next=/ask

urls.py in my root folder:

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 is a view imported from my app qna, which reads:

@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})

The snippet from the tempate that calls it,

<li class="active"><a href="{% url 'qna_ask' %}">Ask Question</a></li>

When I click on this link,I am redirected to /login/?next=/ask which wasn't happening earlier. Even manually typing this url takes me to this faulty url. This is strange. The only change I have added is pagination, which I did as such:

class QuestionListView(ListView):
    ....
    paginate_by = 15
    ...

and in the 'base.html' template

{% if is_paginated %}
<div class="pagination">
    {% if page_obj.has_next %}
    <a class="btn btn-danger" href="?page={{ page_obj.next_page_number }}">More &raquo;</a>
    {% endif %}
</div>
{% endif %}
هل كانت مفيدة؟

المحلول

You are decorating your view with @login_required, if you are not authenticated, you are redirected to the login page. Hope this helps.

نصائح أخرى

You have a @login_required decorator on new_question view function.
Redirection to login page with new_question url (/ask) is an expected behavior.
https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-login-required-decorator

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top