How come I can't include other URLs.py into my Django? - python

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.

Related

How to change the url in urls.py? (Django app)

I'm trying to change the url of http://localhost:8000/stories-and-literature/fairy-tales/ to http://localhost:8000/stories-and-literature/classic-literature/, and I'm not to sure how I can change it in my urls.py. So far, I have this code in urls.py:
from django.conf.urls import patterns, url
from topictree import views
urlpatterns = patterns('',
(r'^$', 'topictree.views.index'),
(r'^(?P<slug>\D+)/$', 'topictree.views.tree')
)
I want to change the slug of url from "fairy-tales" to "classic-literature" because I want to present the url like a tree. In this case, both "fairy-tales" and "classic-literature" are children of "story-and-literature", so when I click on the "fairy-tales" link and then click on the "classic-literature" link, the "fairy-tales" slug should be removed, and be replaced by the "classic-literature" slug.
I was thinking of changing the url in views.py instead of urls.py since views.py contains the control flow of the program. Is it possible to change the url in views.py instead of urls.py?
Any help would be much appreciated.
Foo Bar User's link is a good place to start but judging by your question I would say you should go straight to the Django Docs
https://docs.djangoproject.com/en/1.6/topics/http/urls/
That should get you back on your feet. :)

File serving exclusion error in django urls

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.

Django URL pattern with different roots

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.

Does Django cache url regex patterns somehow?

I'm a Django newbie who needs help: Even though I change some urls in my urls.py I keep on getting the same error message from Django. Here is the relevant line from my settings.py:
ROOT_URLCONF = 'mydjango.urls'
Here is my urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mydjango/', include('mydjango.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
#(r'^admin/doc/', include(django.contrib.admindocs.urls)),
# (r'^polls/', include('mydjango.polls.urls')),
(r'^$', 'mydjango.polls.views.homepage'),
(r'^polls/$', 'mydjango.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mydjango.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mydjango.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mydjango.polls.views.vote'),
(r'^polls/randomTest1/', 'mydjango.polls.views.randomTest1'),
(r'^admin/', include(admin.site.urls)),
)
So I expect that whenever I visit http://mydjango.yafz.org/polls/randomTest1/ the mydjango.polls.views.randomTest1 function should run because in my polls/views.py I have the relevant function:
def randomTest1(request):
# mainText = request.POST['mainText']
return HttpResponse("Default random test")
However I keep on getting the following error message:
Page not found (404)
Request Method: GET
Request URL: http://mydjango.yafz.org/polls/randomTest1
Using the URLconf defined in mydjango.urls, Django tried these URL patterns, in this order:
1. ^$
2. ^polls/$
3. ^polls/(?P<poll_id>\d+)/$
4. ^polls/(?P<poll_id>\d+)/results/$
5. ^polls/(?P<poll_id>\d+)/vote/$
6. ^admin/
7. ^polls/randomTest/$
The current URL, polls/randomTest1, didn't match any of these.
I'm surprised because again and again I check urls.py and there is no
^polls/randomTest/$
in it, but there is
^polls/randomTest1/'
It seems like Django is somehow storing the previous contents of urls.py and I just don't know how to make my latest changes effective.
Any ideas? Why do I keep on seeing some old version of regexes when I try to load that page even though I changed my urls.py?
Django compiles the URL regexes when it starts up for performance reasons - restart your server and you should see the new URL working correctly.

No module named urls

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

Categories

Resources