Domanda

My testProject/urls.py is this

from django.conf.urls import patterns, include, url
from testapp import urls
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testproject.views.home', name='home'),
    # url(r'^testproject/', include('testproject.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),
     url(r'^', include(urls)),
)

and my testApp/urls.py is this

from django.conf.urls import patterns, include, url
from testapp.forms import UsersForm
from templates import login.html

urlpatterns = patterns('',

url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testproject/templates/login.html', 'authentication_form':UsersForm}),

)

Now, when I run the server by doing

python manage.py runserver

it gives me a

SyntaxError at /

error saying

invalid syntax (urls.py, line 3)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.5.2
Exception Type: SyntaxError
Exception Value:    
invalid syntax (urls.py, line 3)
Exception Location: /home/ayman/Documents/djcode/testproject/testproject/urls.py in   <module>, line 2

and the traceback is

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/ayman/Documents/djcode/testproject/testproject/urls.py" in <module>
  2. from testapp import urls

Exception Type: SyntaxError at /
Exception Value: invalid syntax (urls.py, line 3)

Any idea why it is raising a Syntax Error? It's weight because some place it says that the error is in line 3 while other places say it is in line 2. Do note that this was working just a few minutes ago until I decided to change and use Django's generic login view. I had a error passing the Form as an authentication_form and I fixed that error but right after I fixed that error, this Syntax Error was raised.

The previous problem about passing the Form as an authentication_form can be viewed here as a reference

Django generic login view return 'str object not callable' error

just encase it helps.

È stato utile?

Soluzione

There is an error in testapp.urls

from django.conf.urls import patterns, include, url
from testapp.forms import UsersForm
from templates import login.html

urlpatterns = patterns('',

url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testproject/templates/login.html', 'authentication_form':UsersForm}),
)

from templates import login.html is not something you should import since it is not python source. You can do with just removing this line. Because the view only expects a string as parameter for template_name and does not require a Python object.

Altri suggerimenti

Please try the following in testProject/urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
      url(r'^admin/', include(admin.site.urls)),
      url(r'^', include('testapp.urls')),
)

Another way to do this:

from django.conf.urls import patterns, include, url
from testapp.urls import urlpatterns as testapp_urls
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
      url(r'^admin/', include(admin.site.urls)),
      url(r'^', include(testapp_urls)),
)

Also read the following section of the docs, regarding include:
https://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top