Django身份验证和注册

时间:2016-08-18 18:33:17

标签: django

我正在尝试使用Amazon EC2上的Django身份验证和注册系统创建最简单的网站。在遵循Django文档和各种Web教程之后,我仍然会收到此错误:

  

找不到页面(404)请求方法:GET请求URL:       http://megaapp-prod.xxxx.com/appName/

     

使用mysite.urls中定义的URLconf,Django尝试了这些URL    模式,按此顺序:

 <div class="form-check-inline" ng-repeat="cat in category">
      <label class="form-check-label col-xs-4 no-padding" for="categories" ng-true-value="'{{cat.name}}'" ng-false-value="''">
                        {{ cat.name }}
                  <input type="checkbox" 
                  class="form-check-input col-xs-2"
                  id="categories" 
                  ng-model="vmodel.categories[cat.id]" />
      </label>
    </div>
     

当前网址polls / registration /与其中任何一个都不匹配。

这就是我在〜/ w / mysite / polls / views.py

中的内容
^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/

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

中的内容
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,)))

调用DetailView类时似乎发生了错误。

有人可以帮助澄清如何使这项工作?

1 个答案:

答案 0 :(得分:0)

来自文档:https://docs.djangoproject.com/en/1.10/topics/auth/default/#topic-authorization

我在您的代码中看到的是您没有在urls.py中设置/ polls / registration。我假设你做了类似的事情:

FileInputStream inputStream = null;
Scanner sc = null;
try {
    inputStream = new FileInputStream(path);
    sc = new Scanner(inputStream, "UTF-8");
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        // Talk to your database here!
    }
    // note that Scanner suppresses exceptions
    if (sc.ioException() != null) {
        throw sc.ioException();
    }
} finally {
    if (inputStream != null) {
        inputStream.close();
    }
    if (sc != null) {
        sc.close();
    }
}

您需要添加:

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

在您的民意调查中urls.py

现在发生的事情是当使用DetailView时,它会登录但无法找到它,因为它不在网址中。

另外注意,

来自文档:

  

LoginRequired mixin

     

使用基于类的视图时,可以使用LoginRequiredMixin实现与login_required相同的行为。这个mixin应该位于继承列表中最左边的位置。

因此,您正在使用登录装饰器以及LoginRequired