【问题标题】:Getting error while running my APP using python and Django使用 python 和 Django 运行我的应用程序时出错
【发布时间】:2017-11-12 05:08:00
【问题描述】:

我面临一个问题。我是 django 和 python 的新手。当我运行我的应用程序时,我收到以下错误。

    __import__(name)
  File "/opt/lampp/htdocs/mysite/polls/urls.py", line 2, in <module>
    from . import views
  File "/opt/lampp/htdocs/mysite/polls/views.py", line 40

                                                                                     ^
SyntaxError: invalid syntax

我在下面解释我的代码。

view.py:

from __future__ import unicode_literals
from django.shortcuts import get_object_or_404, render
from django.shortcuts import render
from .models import Choice, Question
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)
def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question': question})

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.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):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,))

下面是我的另一个文件url.py

from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

这里我需要知道语法错误的确切位置,请帮我解决这个错误。

【问题讨论】:

    标签: python django


    【解决方案1】:

    你只是在 view.py 的最后一行缺少一个右括号

    return HttpResponseRedirect(reverse('polls:results', args=(question.id,))
    

    应该是,

    return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
    

    【讨论】:

    • 但是当我再次运行http://127.0.0.1:8000/polls/ 时,又出现了一些错误。
    • Request Method: GET Request URL: http://127.0.0.1:8000/polls/ Django Version: 1.11.2 Exception Type: TemplateDoesNotExist Exception Value: polls/index.html Exception Location: /usr/local/lib/python2.7/dist-packages/django/template/loader.py in get_template, line 25 Python Executable: /usr/bin/python Python Version: 2.7.6 Python Path: ]
    • 另一个问题请提供相关细节,或:stackoverflow.com/questions/16870957/template-does-not-exist
    • 这是一个完全不同的问题。看起来 polls/index.html 可能不存在/不在您的项目目录中的正确位置。但这只是一个猜测。进一步调查问题,然后正如@BishwasMishra 所说,创建一个新问题,其中包含更多信息,例如您的模板文件及其位置。
    猜你喜欢
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2018-12-28
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多