2016-08-18 115 views
1

我正嘗試使用Amazon EC2上的Django身份驗證和註冊系統創建最簡單的網站。以下Django文檔和各種網頁教程後,我仍然得到這個錯誤:Django身份驗證和註冊

Page not found (404) Request Method: GET Request URL: http://52.43.72.141/polls/registration/?next=/polls/2/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

^polls/ ^$ [name='index'] 
^polls/ ^(?P<pk>[0-9]+)/$ [name='detail'] 
^polls/ ^(?P<pk>[0-9]+)/results/$ [name='results'] 
^polls/ ^(?P<question_id>[0-9]+)/vote/$ [name='vote'] 
^polls/ ^login/$ [name='login'] 
^polls/^^login/$ [name='login'] 
^polls/^^logout/$ [name='logout'] 
^polls/^^password_change/$ [name='password_change'] 
^polls/^^password_change/done/$ [name='password_change_done'] 
^polls/^^password_reset/$ [name='password_reset'] 
^polls/^^password_reset/done/$ [name='password_reset_done'] 
^polls/^^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm'] 
^polls/^^reset/done/$ [name='password_reset_complete'] 
^admin/ 

The current URL, polls/registration/, didn't match any of these.

這是我在〜/ W/mysite的/調查/ views.py

from django.shortcuts import get_object_or_404, render 
from django.http import HttpResponseRedirect 
from django.core.urlresolvers import reverse 
from django.views import generic 
from django.contrib.auth.mixins import LoginRequiredMixin 
from django.contrib.auth.decorators import login_required 
from .models import Choice, Question 

class IndexView(generic.ListView): 
    template_name = 'polls/index.html' 
    context_object_name = 'latest_question_list' 

    def get_queryset(self): 
     """Return the last five published questions.""" 
     return Question.objects.order_by('-pub_date')[:5] 

class DetailView(LoginRequiredMixin, generic.DetailView): 
    model = Question 
    template_name = 'polls/detail.html' 
    login_url = '/polls/registration/' 

class ResultsView(LoginRequiredMixin, generic.DetailView): 
    model = Question 
    template_name = 'polls/results.html' 

@login_required 
def vote(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    try: 
     selected_choice = question.choice_set.get(pk=request.POST['choice']) 
    except (KeyError, Choice.DoesNotExist): 
     return render(request, 'polls/detail.html', { 
      'question': question, 
      'error_message': "You didn't select a choice.", 
     }) 
    else: 
     selected_choice.votes += 1 
     selected_choice.save() 
     return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 

這是我在〜/ w/mysite/polls/registration/login.html

{% extends "base.html" %} 

{% block content %} 

{% if form.errors %} 
<p>Your username and password didn't match. Please try again.</p> 
{% endif %} 

{% if next %} 
    {% if user.is_authenticated %} 
    <p>Your account doesn't have access to this page. To proceed, 
    please login with an account that has access.</p> 
    {% else %} 
    <p>Please login to see this page.</p> 
    {% endif %} 
{% endif %} 

<form method="post" action="{% url 'login' %}"> 
{% csrf_token %} 
<table> 
<tr> 
    <td>{{ form.username.label_tag }}</td> 
    <td>{{ form.username }}</td> 
</tr> 
<tr> 
    <td>{{ form.password.label_tag }}</td> 
    <td>{{ form.password }}</td> 
</tr> 
</table> 

<input type="submit" value="login" /> 
<input type="hidden" name="next" value="{{ next }}" /> 
</form> 

{# Assumes you setup the password_reset view in your URLconf #} 
<p><a href="{% url 'password_reset' %}">Lost password?</a></p> 

{% endblock %} 

看起來錯誤發生在調用DetailView類時。

有人可以幫助澄清如何使這項工作?

+0

我在目錄民調相同的login.html /模板,民意調查/模板/註冊,民意調查/註冊/模板和民意調查/註冊/模板/註冊 –

+0

錯誤說,它找不到任何匹配模式'/民意調查/註冊'和URL列表的URL表明它沒有e XIST。你有沒有在你的urls.py文件中包含該URL? –

回答

0

從文檔:https://docs.djangoproject.com/en/1.10/topics/auth/default/#topic-authorization

我正在從代碼中看到的是,你沒有/在你的urls.py.民調/註冊成立林假設你不喜歡的東西:

url('^', include('django.contrib.auth.urls')) 

您需要添加:

url('^registration/', views.WHATEVER_VIEW_YOU_WANT_USE) 

從您的民意調查中的urls.py

請告訴我發生的條件是在使用的DetailView,它會登錄但無法找到它,因爲它不在網址中。

也側面說明,

從文檔:

The LoginRequired mixin

When using class-based views, you can achieve the same behavior as with login_required by using the LoginRequiredMixin. This mixin should be at the leftmost position in the inheritance list.

所以,你同時使用的登錄裝飾,還有LoginRequired

+0

非常感謝。這解決了問題。 –