I have a problem with my urls.py file in Django which allows me wherever to have access on my admin interface, wherever to load the images. If somebody could have a look over it, thanks in advance !
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
url(r'^admin/files/(?P<filepath>.*)$', 'my.app.admin.serv_backup_files', name='admin-file-serv'),
(r'^(?P<restaurant_slug>[^(admin)][a-zA-B-_0-9]+)$', TemplateView.as_view(template_name=os.path.join(settings.G_DOC_ROOT, 'index.html'))),
(r'^(?P<path>[^(admin)].*)$', 'django.views.static.serve', {'document_root': settings.G_DOC_ROOT}),
)
The problem is that with this configuration, I see on the console of the runserver, things like
"GET /img/field_bg.gif HTTP/1.1" 404
for all the images, that are supposed to be served statically.
I can remove the [^(admin)] from the last pattern and the site will be served well, except that it will try to reroute the admin interface to the static file.
Thanks in advance for helping me combining the static file, the subdomainless TemplateViewing and the admin normal access.
I don't know python regexes well, but in any other flavor I've ever used, [^(admin)] would match a single character which is anything except '(' OR 'a' OR 'd' OR 'm' OR 'i' OR 'n' OR ')'. A character class ([...]) matches a single character, not a phrase.
If you are trying to NOT match "(admin)", then you can use a negative lookahead like so:
^(?P<restaurant_slug>(?!\(admin\))[a-zA-B-_0-9]+)$
Or, more likely, you are trying to not match "admin":
^(?P<restaurant_slug>(?!admin)[a-zA-B-_0-9]+)$
It looks like you need to add ADMIN_STATIC_PREFIX to your settings and your urls.
Related
I have started learning Django, I'm not sure what the include() function means.
Here is mysite/urls.py. - project
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
Here is polls/urls.py. - app in project
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
From django document, include() function was described as follows.
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.
I'm not sure what is that point, what is remaining string.
In case of the example above, what is remining string, what is url strings which was chopped off?
For example, from this URL:
polls/5/results
the URL rule:
url(r'^polls/', include('polls.urls')),
chops off the polls/ part of URL and sends the remaining string after polls/, whatever it might be, for example (see here more):
5/results/
to urls from the poll app's urls.py, where it will then be mapped to a view based on the URL rules defined in this file
Whenever it will encounter any url with /polls then it will include all the urls of polls app.
Example:
If you type /polls/hey
Then as soon as it sees /polls it will go to polls urls file and later it will search for:
hey/ matching over there.
Lets say there is one more entry in your polls/urls.py like
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
here year is the query string parameter. so your url will look like
/polls/articles/2007 so in this case /polls/articles/ will matched up and 2007 will pass to year_archive method
In your example there is no chopped string, the URL comes back as simply polls/, but when you have another url such as '^new$' then that url is being chopped, merged with polls/ and it returns polls/new, hope this makes sense..
I'm currently learning Django and I'm trying to add form to register a User.
Now my problem starts really early because I get a 404 whenever I try to access my registration page.
My 3 files are as follows:
views.py : just a hello world to display basically
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
def registration(request):
return HttpResponse("Registration page")
urls.py (in my project) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^$', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
and finally urls.py (in my app) :
from django.conf.urls import patterns, url
from myapp import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
url(r'^registration/$',views.registration, name='registration'),
)
When I go to my normal index (localhost:8000) It displays the hello world I wrote in the views.py file. However when I try to go the registration page it just returns me a 404 (localhost:8000/registration).
Now as I said I'm still learning this whole thing, but I don't get why It doesn't work properly. As far as i understand the r'^$' in the project file is pointing towards the localhost:8000 and the same regex in the app file tells it to load the index page in this location. Now as the program is still in localhost:8000, a r'^registration/' should be loading the other page in localhost:8000/registration right?
It would be really nice if you could explain to me why this doesn't work and where I did a mistake in my thoughts.
This is the error I get:
When i take
(r'^randomstringhere/', include('myapp.urls')), # index with login/registration
instead of
(r'^$', include('myapp.urls')), # index with login/registration
I can get to the registration page via localhost:8000/randomstringhere/registration. But Site is meant to have a selection where you can either Log in or register on localhost:8000 (or future domain www.randomdomainhere.com) and a registration/login form on localhost:8000/login, localhost:8000/registration (www.randomdomainhere.com/registration etc.)
At the end of each url you should add a $ symbol,
url(r'^registration/$',views.registration, name='registration'),
reffer this https://docs.djangoproject.com/en/1.4/topics/http/urls/
you are missing $ sign in the end of url.
It should be:
url(r'^registration/$',views.registration, name='registration'),
The $ symbol in django urls means end-of-string match character which is borrowed from regular expression. So, $ should be added at the end of your url on app's urls.
url(r'^registration/$',views.registration, name='registration'),
However we can write urls with the regular expressions that don’t have a $ (end-of-string match character) but we do have to include a trailing slash(/). For urls without $, you must enter the trailing slash while opening the url in your browser to match the regular expression.
Your app urls in project's urls.py is
url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
So, to access the registration page, you need to browse by
localhost:8000/grappelli/registration/
i.e. append the path in your app's url to the path in project's url for the app.
Learn more about urls here.
You need to remove $ from the first url as given below
urls.py (in my project) :
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
(r'^', include('myapp.urls')), # index with login/registration
(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
(r'^admin/', include(admin.site.urls)), # admin site
)
A url starts with ^ and ends with $. so if $ is encountered django understands that the url has ended and there's nothing more infront of that. so in your case it will no more look for "registration". or if your url with "registration" doesnt matches to any urls specified at all.
I have two URL patterns that both exist in the same application that I'm working on getting set up.
I need urls like the following to work.
http://www.domain.com/p/12345
http://www.domain.com/s/12345
However, both of these live in the same django application.
My main urls.py looks something like this for handling the /p/12345 urls.
urlpatterns = patterns('',
(r'^p/', include('myproject.myapp.urls')),
)
and my urls.py for the application is similar. but this still only handles the /p/12345 urls.
urlpatterns = patterns('myproject.myapp.views',
(r'^(?P<object_id>\d+)/$', 'some_view'),
)
My issue is that both are almost identical but just have a different prefixes. How can I do this for both the /p/12345 and /s/12345 urls. I've read through the documentation but wasn't able to figure this one out. I've thought of 'sloppy' ways to do this with 2 urls.py files, but I know there must be a better way.
You can include a URLs file with an empty pattern. You could do this:
main urls.py
urlpatterns = patterns('',
(r'foo/', 'foo_view'),
(r'^', include('myproject.myapp.urls')),
)
app urls.py
urlpatterns = patterns('puzzlequest.pq.views',
(r'^p/(?P<object_id>\d+)/$', 'some_view'),
(r'^s/(?P<object_id>\d+)/$', 'other_view'),
)
Note that other routes (like foo/) have to come first.
I have a urls.py. One of them says this line:
(r'^notification/?$',include("notification.urls")),
I'm supposed to do that because I installed "django_notification" (and added "notification") to INSTALLED_APPs.
Great! going to /notification/ works! This is the urls.py in the notification module:
from django.conf.urls.defaults import *
from notification.views import notices, mark_all_seen, feed_for_user, single, notice_settings
urlpatterns = patterns('',
url(r'^$', notices, name="notification_notices"),
url(r'^settings/$', notice_settings, name="notification_notice_settings"),
url(r'^(\d+)/$', single, name="notification_notice"),
url(r'^feed/$', feed_for_user, name="notification_feed_for_user"),
url(r'^mark_all_seen/$', mark_all_seen, name="notification_mark_all_seen"),
)
However, only /notification works and it displays the word "notice" when I hit that url. Nothing else works. /settings, /feed. None of that work. I get a 404 error that Django tried all the URLs in order.
Perhaps it's because of the "notification_notice" thing??
If you read the documentation, you'll see that the include will remove the part that matches, so you'd have to go to /notification/settings, and /notification/feed
Solved.
I removed the ?$ at the end of the url match.
I'm following the Django Tutorials, I'm at the end of part 3, at Decoupling the URLconfs, at http://docs.djangoproject.com/en/1.1/intro/tutorial03/#intro-tutorial03 and I'm getting a "No module named urls" error message.
When I change:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('mysite.polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
(r'^admin/', include(admin.site.urls)),
)
to:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
I changed include('mysite.polls.urls')), to include(mysite.polls.urls)),, but it still didn't work.
How to solve this problem?
UPDATE 2: at mysite/polls/urls.py is
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
UPDATE 4: the whole project is at
http://www.mediafire.com/?t1jvomjgjz1
I had a similar problem in my project root ... django complained that it couldn't find the module mysite.urls.
Turns out my ROOT_URLCONF variable in settings.py, which was set up using the default values, was set incorrect. Instead of "mysite.urls", it should have been simply "urls"
I changed it, and voila, it worked.
I can't re-produce the import error on my machine using your project files (Windows 7, Django 1.1.1, Python 2.6.4). Everything imported fine but the urls were not specified properly (like the tutorial shows). Fixing the code:
/mysite/urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
/mysite/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Visit http://127.0.0.1:8000/polls/ - I received a TemplateDoesNotExist exception because the template file is missing.
I'm afraid my answer might be to reboot and try it again. ;)
Is there an __init__.py inside mysite/polls/ directory?
I also had a weird problem with "No module named mysite.urls".
The admin site was down and the my whole site.
The solution, after a few hours of searching the web, was on my side : Django is caching some of the settings in a file that he knows from an environment variable.
I just closed my terminal in which i was doing the runnserver thing and opened a new one.
I did exactly the same thing. Python newbie mistake by reading ahead. I created a file call "polls.url" thinking it was some sort of special django template file.
I misunderstood the text:
"Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls.urls file should now look like this:
"
It should really read:
"Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls/urls.py file should now look like this:
"
Late to the party but I fixed my problem by correcting a typo inside settings.py INSTALLED_APPS. I had 'webbapp' instead of 'webapp' so you might want to check on that as well (for people still having the problem)
Like Ryan Bagwell, problem was (at least temporarily) solved by changing ROOT_URLCONF in myproject/myproject/settings.py.
I had to add "myproject" to ROOT_URLCONF to have there this:
"%s.myproject.urls" % PROJECT_APP