-1

I am facing one issue. I am new to django and python. While I am running my APP I am getting the below error.

    __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

I am explaining my code below.

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

My another file url.py is given below.

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

Here I need to know actually where the syntax error is coming and Please help me to resolve this error.

1 Answers1

1

You are simply missing a closing parenthesis in the last line of view.py

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

Should be,

return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
weirdev
  • 1,388
  • 8
  • 20
  • but here when i am running `http://127.0.0.1:8000/polls/` again some error is coming. –  Jun 10 '17 at 06:45
  • `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: ]` –  Jun 10 '17 at 06:50
  • Another question with relevant details please, or: https://stackoverflow.com/questions/16870957/template-does-not-exist – Bishwas Mishra Jun 10 '17 at 06:51
  • This is a completely different issue. It looks like polls/index.html may not exist/be in the right location in your project directory. But that is just a guess. Investigate the problem further, then as @BishwasMishra said create a new question that includes more information, such as your template files and their locations. – weirdev Jun 10 '17 at 06:56