Django Admin Error - unbalanced parenthesis - python

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 $.

Related

Django some URLs don't work

I've been making some URLs in Django, including them and so on. Everything looks fine, but no matter what i do i always end up with a 404 on some of the simplest URLs.
For example I can browse myapp/0 abd myapp/1/details/, but i get 404'd at myapp/foo
So here are my urlconf :
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
and myapp urlconf :
urlpatterns = [
url(r'^foo/$ ', FooView.as_view()),
url(r'^(?P<bar_id>\d+)/$', BarByIdView.as_view()),
url(r'^(?P<bar_id>\d+)/details/$', BarDetailsByIdView.as_view()),
]
And when i tryp myapp/foo Django shows me the following urls list :
^admin/
^myapp/ ^foo/$
^myapp/ ^(?P<bar_id>\d+)/$
^myapp/ ^(?P<bar_id>\d+)/details/$
In the myapp.conf you have added a / at the end of the url pattern
url(r'^foo/$ ', FooView.as_view()
This must be viewed with /myapp/foo/ and not /myapp/foo because the first one matches the regex where as the second one won't.
Your URL is /myapp/foo/, not /myapp/foo. Note the trailing slash.
If you want both to work, ensure you have the APPEND_SLASH setting set to True and the CommonMiddleware enabled.

Django URL ordering

I'm trying to use the Django sitemap framework to set up a sitemap but I'm getting an issue with setting up the sitemap URL.
Here is my projects urls.py
from django.contrib.sitemaps.views import sitemap
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^debug-toolbar/', include(debug_toolbar.urls)),
)
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
url(r'^robots\.txt$', TemplateView.as_view(template_name="robots.txt")),
url(r'^500/$', TemplateView.as_view(template_name="500.html")),
url(r'^404/$', TemplateView.as_view(template_name="404.html")),
url(r'^', include('hunt.urls')),
)
else:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}),
url(r'^robots\.txt$', TemplateView.as_view(template_name="robots.txt")),
url(r'^500/$', TemplateView.as_view(template_name="500.html")),
url(r'^404/$', TemplateView.as_view(template_name="404.html")),
url(r'^', include('hunt.urls')),
)
Here is my hunt apps urls.py
urlpatterns = patterns('hunt.views',
url(r'^$', top100, name='top100'),
url(r'^explore/$', explore, name='explore'),
url(r'^monthly/$', monthlytop10, name='monthlytop10'),
url(r'^trending/$', trending, name='trending'),
url(r'^genres/$', genres, name='genres'),
url(r'^genres/(?P<slug>.+)/$', genre_view, name='genre_view'),
url(r'^contact/$', contact, name='contact'),
url(r'^contact/thanks/$', thanks, name='thanks'),
url(r'^faq/$', faq, name='faq'),
url(r'^songs/(?P<slug>.+)$', song_search, name='song_search'),
url(r'^loadmore/dj/$', loadmore_dj, name='loadmore_dj'),
url(r'^loadmore/genre/$', loadmore_genre, name='loadmore_genre'),
url(r'^loadmore/month/$', loadmore_month, name='loadmore_month'),
url(r'^loadmore/trending/$', loadmore_trending, name='loadmore_trending'),
url(r'^vote/dj/$', vote_dj, name='vote_dj'),
url(r'^vote/genre/$', vote_genre, name='vote_genre'),
url(r'^vote/month/$', vote_month, name='vote_month'),
url(r'^vote/trending/$', vote_trending, name='vote_trending'),
url(r'^(?P<slug>.+)/$', dj, name='dj'),
)
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
When I try to access sitemap.xml I'm getting an error
404 Page Not Found
No DJ matches the given query.
This means it tried to access the last URL in my hunt app. Why is it trying to access the DJ Page even though it comes after sitemap.xml URL?
EDIT -
As per eran's suggestion, I ran the command show_urls of django_extensions. Here is the output of that
/ hunt.views.top100 top100
/404/ django.views.generic.base.TemplateView
/500/ django.views.generic.base.TemplateView
/<slug>/ hunt.views.dj dj
/contact/ hunt.views.contact contact
/contact/thanks/ hunt.views.thanks thanks
/explore/ hunt.views.explore explore
/faq/ hunt.views.faq faq
/genres/ hunt.views.genres genres
/genres/<slug>/ hunt.views.genre_view genre_view
/loadmore/dj/ hunt.views.loadmore_dj loadmore_dj
/loadmore/genre/ hunt.views.loadmore_genre loadmore_genre
/loadmore/month/ hunt.views.loadmore_month loadmore_month
/loadmore/trending/ hunt.views.loadmore_trending loadmore_trending
/media/<path> django.views.static.serve
/monthly/ hunt.views.monthlytop10 monthlytop10
/robots.txt django.views.generic.base.TemplateView
/sitemap.xml django.contrib.sitemaps.views.sitemap
/songs/<slug> hunt.views.song_search song_search
/static/<path> django.contrib.staticfiles.views.serve
/trending/ hunt.views.trending trending
/vote/dj/ hunt.views.vote_dj vote_dj
/vote/genre/ hunt.views.vote_genre vote_genre
/vote/month/ hunt.views.vote_month vote_month
/vote/trending/ hunt.views.vote_trending vote_trending
According to that show_urls output, the URLpattern for hunt.views.dj is taking precedence, because the r'^(?P<slug>.+)/$' regex appears before the sitemap.xml regex. The former regex will match a request to "sitemap.xml", so Django will never reach the pattern that's specifically meant for sitemap.xml. For that matter, it will never reach /contact/, /explore/, /faq/, etc.
I suspect that's happening because your project-wide urls.py appends to urlpatterns ("urlpatterns +=") rather than setting it directly. Looks like you didn't paste all of the imports from the project-wide urls.py, but I would assume one of the things you're importing includes a patterns object; otherwise you'd be getting a NameError at import time (name 'patterns' is not defined).
You should be able to fix it by changing this...
urlpatterns += patterns('',
...to this...
urlpatterns = patterns('',
But I have no way of knowing for certain, because I don't know what else you've imported in the project's urls.py.
I had a sitemaps.xml file in my templates folder that I was using before. Deleting that solved the issue.
I notice that your sitemap and robots.txt urls are the only ones that don't end in a trailing slash. Many web servers will redirect myurl.com/item to myurl.com/item/ - for example, this is the preferred practice when using Apache (search for "Trailing Slash Problem"). Try doing a curl to the url without a trailing slash - do you get a 301 redirect? If so then it is likely redirecting to the URL which DOES include a trailing slash, which is not matched by a pattern in your urls.py. Try adding trailing slashes to your regular expressions, or setting APPEND_SLASH = True in settings.py

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 Invalid Syntax Error

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.

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