/ rango /中的SyntaxError,语法无效

时间:2015-11-02 19:35:57

标签: python django

我正在关注tangowithdjango django教程。虽然我正在运行http://127.0.0.1/rango但我收到了以下错误。 错误是

  Django Version:   1.8.5
  Exception Type:   SyntaxError
  Exception Value:  invalid syntax (views.py, line 15)

  Exception Location:   /root/work/tango_with_django_project/rang/urls.py in <module>, line 2

我的urls.py是

from django.conf.urls import patterns,url
from rango import views
urlpatterns = patterns('',
     url(r'^$', views.index, name='index'),
     url(r'^about/$', views.about, name='about'),
     url(r'^category/(?P<slug>[\w\-]+)/$', views.category, name='category'),) 

和views.py是

from django.shortcuts import render
from rango.models import Category,Page

from django.http import HttpResponse
def index(request):
     category_list = Category.objects.order_by('-likes')[:5]
     context_dict={'categories':category_list}
    return render(request, 'rango/index.html', context_dict)
def about(request):
    return HttpResponse(" <a href='/rango/'>go back</a>")
def category(request,slug):
    context_dict={}
    try:
       my_category = Category.objects.get(slug=category_name_slug)
       context_dict['category_name'] = my_category.name

       pages = Page.objects.filter(category=my_category)


       context_dict['pages'] = pages

       context_dict['category'] = my_category
    except Category.DoesNotExist:

       pass


    return render(request, 'rango/category.html', context_dict)

追溯是

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/rango/about/

Django Version: 1.8.5
Python Version: 2.7.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rango']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:
 File "/usr/local/lib/python2.7/dist-packages/django/core/handlers   /base.py" in get_response
  119.                 resolver_match = resolver.resolve(request.path_info)
   File "/usr/local/lib/python2.7/dist-packages/django  /core/urlresolvers.py" in resolve
  365.             for pattern in self.url_patterns:
 File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
 401.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
 File "/usr/local/lib/python2.7/dist-packages/django  /core/urlresolvers.py" in urlconf_module
 395.             self._urlconf_module = import_module(self.urlconf_name)
 File "/usr/lib/python2.7/importlib/__init__.py" in import_module
 37.     __import__(name)
 File "/root/work/tango_with_django_project/tango_with_django_project/urls.py" in <module>
 23.     url(r'^rango/',include('rango.urls')),
 File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
 33.         urlconf_module = import_module(urlconf_module)
 File "/usr/lib/python2.7/importlib/__init__.py" in import_module
 37.     __import__(name)
 File "/root/work/tango_with_django_project/rango/urls.py" in <module>
 2. from . import views

 Exception Type: SyntaxError at /rango/about/
 Exception Value: invalid syntax (views.py, line 15)

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

你的身份是错的,请查看:

    # Retrieve all of the associated pages.
    # Note that filter returns >= 1 model instance.
    pages = Page.objects.filter(category=category)

    # Adds our results list to the template context under name pages.
    context_dict['pages'] = pages
    # We also add the category object from the database to the context dictionary.
    # We'll use this in the template to verify that the category exists.
    context_dict['category'] = category

现在你有一个缩进错误:

    def index(request):
         category_list = Category.objects.order_by('-likes')[:5]
         context_dict={'categories':category_list}
        return render(request, 'rango/index.html', context_dict)