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
Related
i want to try another apps in django but i got problem when access another apps.
Page not found
tree:
main:
search:
index.html
scrape.html
preprocessing:
index.html
the scenario like this. from scrape.html i want to access index.html in preprocessing but got an error path not found. I've done to add apps in settings.py
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index,),
path('scrape/',views.scrape,),
]
slice of scrape.html:
Preprocessing
preprocessing url.py
path('', views.index),
let me know where did I go wrong, thx for your help
Your anchor tag is written as:
<a href = "/preprocessing" ...>
The problem here is that your url pattern is like 'preprocessing/', notice that it ends in a trailing slash while your anchors url doesn't. Furthermore in your settings you set APPEND_SLASH = False.
Normally when Django finds a url not ending in a trailing slash it appends a slash to it and redirects the user to this new url. But you have stopped this behaviour by setting APPEND_SLASH = False. As the first step I would advice you to change this back to APPEND_SLASH = True.
Next you should always name your urls and use those names to refer to the urls. So your urlpatterns should be:
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index, name='index'),
path('scrape/',views.scrape, name='scrape'),
]
preprocessing url.py
path('', views.index, name='preprocessing'),
Now in your templates you would simply use the url template tag:
Preprocessing
So, I am not really lucky with latest django version tutorials, so I've had some problems with things that changed between some versions. One of this things is: althought I do exactly as I read/watch in the tutorials I always get the same result - all urls redirect to the same HTML page.
Here is my root urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('theblog.urls')),
]
Here is my app urls:
from django.conf.urls import url, include
from .views import HomeView, ArticleDetailView
urlpatterns = [
url('', HomeView.as_view(), name='home'),
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
]
For example, when I go to localhost:8000/articles/1 (or any other pk), it renders home.html (HomeView class) as if it was localhost:8000/.
Hope you can help me. Thanks!
There are two things wrong with your code.
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
This will not work. If you want to use url. you can't use <int:pk>, you need to use a RegEx:
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail")
Note that this will be deprecated in the future and if you are using django >=2.0 you should use path:
path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail")
However this will still direct you to the wrong view. django stops after the first URL pattern match.
Switch them around to fix that:
urlpatterns = [
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail"),
# path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail"), # alternative with path instead of url
url('', HomeView.as_view(), name='home')
]
It may be due to this line
url('', HomeView.as_view(), name='home'),
Because url wraps re_path there may be some logic which will treat the blank regex string as a wildcard. Try changing it to '/'
url('/', HomeView.as_view(), name='home'),
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 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.