Django Invalid Syntax Error - python

I am at my wits. I am trying to set up Django admin but I am having a hard time. I have followed the Django book and checked several times but get an invalid syntax (urls.py, line 23).
from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^hello/$', hello),
(r'^time/$', current_datetime),
(r'^time/plus/(\d{1,2})/$', hours_ahead),
(r'^admin/', include(admin.site.urls),
)
Any insight would be appreciated - thanks!

You are missing a closing parens on line 22:
(r'^admin/', include(admin.site.urls)),
When you get a SyntaxError in Python, check that your parenthesis are balanced on the lines preceding it.

Related

Django Syntax Error in url.py file

I am working through the getting started with django project and keep getting a syntax error at the end of the first video when running locally:
SyntaxError at /
invalid syntax (urls.py, line 12)
Request Method: GET
Django Version: 1.5
Exception Type: SyntaxError
Exception Value:invalid syntax (urls.py, line 12)
My urls.py file is:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$",TemplateView.as_view(template_name="index.html"))
url(r'^admin/', include(admin.site.urls))
)
Thanks for your help
You are missing a comma:
url(r"^$",TemplateView.as_view(template_name="index.html"))
# no comma at the end here --------------------------------^
url(r'^admin/', include(admin.site.urls))
Without Python sees url() url() side by side without a delimiter.
Corrected code:
urlpatterns = patterns('',
url(r"^$",TemplateView.as_view(template_name="index.html")),
url(r'^admin/', include(admin.site.urls))
)
It needs to look like this:
urlpatterns = patterns('',
url(r"^$",TemplateView.as_view(template_name="index.html")),
url(r'^admin/', include(admin.site.urls))
)
notice the comma at the end of the second line
Have you already created an app using "python manage.py startapp "? If so, you should be able to just replace the TemplateView.as_view.... with ".views." if you have a function to handle the viewing (view function docs). Your function would end up looking something like this:
def pageIndex(request):
return render_to_response("index.html")
and your urlconf would be
url(r"^$","<appname>.views.pageIndex")

Django Admin Error - unbalanced parenthesis

When I point my browser to localhost:8000/admin, I get
error at /admin/
unbalanced parenthesis
The code that produces this error:
Project - urls.py:
urlpatterns = patterns('',
(r'^host/', include('host.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Host (app) - urls.py:
urlpatterns = patterns('host.views',
url(r'^all/$', 'EventsAll'),
url(r'^get/(?P<event_id>\d+)/$)', 'Event'),
)
However, if I disable
url(r'^get/(?P<event_id>\d+)/$)', 'Event'),
the admin console works perfectly. Is the regex somehow interfering with the parenthesis?
You have one open parenthesis and two close parenthesis in that regular expression. I'd remove the one after the $.

Django-registration - error No module named urls

I am trying this with Django 1.4 on Windows 7 with the default web server.
The site starts with no errors. but when I browse to localhost:8000
I get the following error
ImportError at /
No module named urls
I see where the error comes from
It is in the main URLs.py file - (r'^admin/', include('django.contrib.admin.urls')),
If I remove that form URLs.py file the home page comes up. I don't see urls.py file in "C:\Python27\Lib\site-packages\django\contrib\admin" folder. So, the error makes sense.
But that line has to be there to get Django-Registration package working. All the blogs I read about has that line. How do I get pass this? Thanks so much for your error.
Fixed the url.py per Siva's instructions below. but no luck.
from django.conf.urls import patterns, include, url
from SOWLAPP.views import *
from CATALOG.views import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'SOWL.views.home', name='home'),
# url(r'^SOWL/', include('SOWL.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'^user/(\w+)/$', user_page),
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^catalog/$', home),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root' : 'C:/SHIYAM/Personal/SuccessOwl/SOWL0.1/SOWL/SOWL/static'}),
(r'^admin/', include('django.contrib.admin.urls')),
(r'^accounts/', include('registration.urls')),
(r'^$', main_page),
)
Regards,
SHM
Check the ROOT_URLCONF entry in your settings file. The following links might help you.
https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
How to set correct value for Django ROOT_URLCONF setting in different branches
But Your urls.py looks messy. 2 entries for same url?
url(r'^admin/', include(admin.site.urls)),
(r'^admin/', include('django.contrib.admin.urls')),
The following entry should comes at the end.
(r'^$', main_page),
Try to comment out each line inside:
urlpatterns = patterns('',
...
)
if it does not solve the error Than (as I've expected) error in the urls.py imports...
I suspect they are somewhere here:
from SOWLAPP.views import *
from CATALOG.views import *
Try to render main page without those modules imported in the urls.py
Also please provide your project settings.py config. That is under section INSTALLED_APPS. There might be an app that has no urls.py and something is referring there. But it's last case that may be... (IMHO)
As the second thought you might have the corrupted/changed Django or/and some apps redistributable version... Check if you installed them via automatic scripts. They rarely have serious bugs.

Django Generic Views Date-Based URLconf

Trying to teach myself Django but running into a snag.
Generic Views seem to be a great idea but I personally find the documentation a little cryptic at times (maybe I'm being prissy).
So I have been trying to use the Date Based generics views in and specifically ArchieveIndexView.
I have even attempted following some non-djangoproject.com examples and still have problems.
I used the example provided at this site.
Here is my current project/urls.py.
I am also at this point, not worrying about pattern matching, just trying to get it to work.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
With this setup I keep receiving a Invalid Syntax error at the line describing ArchiveIndexView class.
If I comment out this line the problem goes away. If I decouple the URLs to their appropriate app I get the same error.
The error suggest I just have something out of place, a comma or something but I have yet to conclude what it is.
Thank you!
use the below code
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view({'date_field': 'pub_date', 'queryset': Entry.objects.all()})),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
You seem to forget the {} brace required for dict in url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
line.
Ah. I solved my own question thanks to a little pushing from shiva.
The dictionary works but only for the extra_content argument.
It was just done like that on the website I was trying to copy and for extra content in the documentation so I kept overlooking that glaringly obvious problem.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view(date_field='pub_date', queryset=Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Just needed to sleep on it...

What's wrong with this url?

I have a root urls.py and an app urls.py. In my root I have this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', include('realestate.properties.urls')),
(r'^admin/', include(admin.site.urls)),
)
In my app urls I have the following
from django.conf.urls.defaults import *
urlpatterns = patterns('realestate.properties.views',
url(r'^$', 'property_list', {'template_name': 'properties/property_list.html'}, name='property_list'),
url(r'^(?P<slug>[-\w]+)/$', 'property_detail', { 'template': 'property_detail.html' }, name='property_details'),
)
now in my template I have a link to the details view, which looks like this:
{% url property_details property.slug %}
Everytime I render this page i get the error:
*Caught NoReverseMatch while rendering: Reverse for 'property_details' with arguments '(u'111-front-st',)' and keyword arguments '{}' not found.*
No matter what I do, I get that error. I tried capturing just the id and nothing is working, i am not sure why, I have used url's many times before so I am really confused if I am missing something obvious. Any see anything wrong here?
Thanks
Jeff
I think you need to drop the $ from your urlconf, where you include the app's urls. Probably you can remove the ^ too.
urlpatterns = patterns('',
(r'^', include('realestate.properties.urls')),
(r'^admin/', include(admin.site.urls)),
)
http://docs.djangoproject.com/en/1.2/topics/http/urls/#including-other-urlconfs
Note that the regular expressions in
this example don't have a $
(end-of-string match character) but do
include a trailing slash. Whenever
Django encounters include(), it chops
off whatever part of the URL matched
up to that point and sends the
remaining string to the included
URLconf for further processing.
Do something like this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^$', 'realestate.properties.views.property_list'),
(r'^properties/', include('realestate.properties.urls')),
(r'^admin/', include(admin.site.urls)),
)
Otherwise (like sugested by Reiners post) you will make the first regular expression a "catch all" and /admin will never match.
You can also place the admin regular expression before your "catch all" re, but what happens if you have a slug like 'admin'? That is why I would advice against a url scheme with /<slug>/ at the first level. Instead, use /<object-type>/<slug>/, that will make room for other things in the future.

Categories