我想在Django中解决问题

时间:2019-01-10 06:23:36

标签: python django webserver

今年秋天我还没有解决。我不知道这个错误是什么意思。请解决此错误。

问题:enter image description here

path('/polls', views.polls, name='polls'),

\src\fistsite\urls.py", line 7, in module

AttributeError:   module 'polls.views' has no attribute 'polls'

源代码

urls.py-fistsite

from django.contrib import admin
from django.urls import path, include
from polls import views

urlpatterns = [
    path('', views.index, name='index'),
    path('/polls', views.polls, name='polls'),
    path('/admin', views.admin, name='admin')
]

urls.py-polls

from django.urls import path
from . import views


app_name = 'polls'
urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/result/', views.results, name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote')
]

views.py-polls

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from polls.models import Question, Choice

# Create your views here.
def index(request):
    latest_question_list = Question.objects.all().order_by('-pub_date')[:5]
    context = {'latest_question_list':latest_question_list}
    return render(request, 'polls/index.html', context)

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

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,)))

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/result.html', {'question': question})

1 个答案:

答案 0 :(得分:0)

问题在这里是您正在使用views.polls,但是在views.py中却具有索引,详细信息,投票和结果。 views.py中没有民意调查。 相反,您需要包括民意调查应用程序中存在的所有urlpattern,

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', views.admin, name='admin'),
]

再次遍历官方文档。