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.
Related
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^logs/', include(logs.urls)),
]
I have written this code in main/urls.py file
code in logs/urls.py is below:-
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$/', views.index, name='index'),
]
and i am accessing http://localhost:8000/logs
Error:- NameError name 'logs' is not defined
You need to import the module you are actually referencing before adding it to your URLs file.
Assuming logs is in your import path:
import logs
Usually you would use a string with include so that you don’t have to import the module;
url(r'^logs/', include('logs.urls')
Also, you should remove the slash from the end of your regex for the index view. The dollar marks the end of the string, so r'^$/' will never match.
url(r'^$', views.index, name='index'),
Please check the docs. It should help!
include() function takes a list/tuple of url(), path(), re_path() and includes them to django routing system.
It also requires app's name: you can define it in include(("enter here your app's name", app.urls))
or in an app itself, in urls.py at a modular level. app_name = 'enter here your apps'name '
But only include() isn't enough, you have to define it in path() or smth that is used for routing (e.g re_path(), url() (deprecated in django 2.0))
For full details: include() function.
So, the full poiting to app's urls look so:
from django.conf.urls import url, include
from . import views
from . import app
urlpatterns = [
url(r'^', include(("app", app.urls)), name='index'),
]
I recommend you to use path() and re_path() if you use Django 2.0 >
I noticed that you use regex incorrectly little bit
url(r'^$/', views.index, name='index'),
You type $ before /, it's incorrect, $ means the end of expression and must be used in the end of expression.
Unfortunately I can't tell you all the details in the post. You should read Django docs. Go through django tutorial for start.
I'm sure it will help!
I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page
For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.
Here is my code:
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('portal.urls')),
url(r'^login/', include('login.urls')),
]
login urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/', views.index, name="login"),
]
login views.py
from django.shortcuts import render
def index(request):
return render(request, 'login/login.html')
portal urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name="portal"),
]
I see 2 problems here:
As #DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.
You don't terminate the portal index URL, so it matches everything. It should be:
url(r'^$', views.index, name="portal"),
In addition, if the regex is login/$ but you enter http ://server/login, then it won't match wheras http://server/login/ will.
You could try changing the regex to login/*$, which will match any number (even zero) / on the end of the url.
So http: //server/login, http: //server/login/, http: //server/login//// would all match.
Or if you want to be specific, login/{0,1}$ might work (though that regex syntax is from memory!)
I'm developing a webpage with Django.
I want to add/define an url for the application.
At this moment the url (dsvd/) doen't work properly. It shows only table data but no css and background.
here is the code for the main urls.py file.
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'kleedkamer_overview.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('kleedkamer_overview.urls')),
url(r'^dsvd/', 'kleedkamer_overview.views.indeling'),
)
and here the code inside the application urls.py file
from django.conf.urls import patterns, url
from kleedkamer_overview import views
urlpatterns = patterns ('',
url(r'^$',views.indeling, name='indeling'),
)
Is there anyone who can help me find the problem?
Greetz.
It looks like line is giving you trouble:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
It seems like you are trying to map this URL to kleedkamer_overview.views.indeling but the URL regrex does not end with a '$'. Urls.py files that are mapped to other apps look like this:
url(r'^admin/', include(admin.site.urls))
Notice the include function call and that the regrex expression r'^admin/' does not end with a $
However mapping to a specific view is a bit different and it looks like this:
url(r'^$', 'kleedkamer_overview.views.home', name='home')
Notice that this time, where the include() function call would go you are instead telling Django which specific view you want to use and that the site regrex is r'^$' ending with a $.
Try changing this:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
to this:
url(r'^dsvd/$', 'kleedkamer_overview.views.indeling')
Edit:
I saw your comments and while the urls.py is not the source of your problem, what I said is still valid because the urls.py was malformed. You should not even have the offending line there at all because you have already included the urls.py from kleedkamer_overview , there is no need to include it twice. It isn't DRY and it is just bad practice in general. This is why this still works despite being malformed because sites are looked for in order, in this case first it looks for /admin then / and does not reach /dsvd because it was already caught by / and your malformed url mapping is NEVER reached.
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
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")