The pages aren't found for an app - django

Main url file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
My app url file:
from django.conf.urls import patterns, url
from mainapp import views
urlpatterns = patterns('',
url(r'^$', views.index),
url(r'^url1$', views.url1),
url(r'^url2$', views.url2)
)
When I go to "/", it shows the views.index from my app fine. However, it says "Page not found (404)" for url1 and url2.
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^$
^admin/
The current URL, url1, didn't match any of these.
What's up with that?

You are matching '^$', which is an empty string, so your other urls are not called.
Try:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('myapp.urls'))
)
Note that I changed the ordering. This is so that myapp will never override other, more specific urls like admin.

r'^url1$'
^ means start here;
$ means end here.
so, it is an empty string. it does not make sense to put anything between like you did ^ ... $.
how about to try: r'^$', 'views.url1'),
?

Related

Need a seconda pair of eyes on Django URL dispatcher

Wondering if I could get some help with a second pair of eyes. Started working Django about two months ago learning in my spare time. Long story short: My index and post view work fine. I recently created a contact html\template and view which looks like it goes right back to my index page? Here is my project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from .settings import MEDIA_ROOT
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^contact/', include('blog.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
)
Here is my app urls.py
from django.conf.urls import patterns, include, url
from blog import views
urlpatterns = patterns('blog.views',
url(r'^(?P<post_name>\w+)/$', views.post, name='post'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^$', views.index, name='index'),
)
If I change my contact view to come up as my default the view it comes up fine.
urlpatterns = patterns('blog.views',
url(r'^(?P<post_name>\w+)/$', views.post, name='post'),
url(r'^$', views.contact, name='contact'),
#url(r'^$', views.index, name='index'),
)
As soon as switch it back to the original state when I click on the contact link on my page it goes right to my index page. I have to be doing something wrong with the URL dispatcher part but I'm not sure what.
Your contact urlpattern is not matching the URL "/contact", it is matching "/blog/contact" and "/contact/contact". Since you've included the blogs urlpatterns under both those prefixes, the thing that matches "/contact" is the URL without anything after the prefix, ie the index.
You probably don't want to put the contact pattern into the included file at all: just match it directly in the base project file.
Please mind the order of the urls in your app's urls.py. Try to put your views.contact view before your views.post view. Keep in mind: Django chooses the first matching regular expression, which is your views.post view.
I ran into a similar problem and described the solution here.
Here your contact view has the url /contact/contact/ and index view has the url /contact/
So you have rewrite the project urls as
from django.conf.urls import patterns, include, url
from django.contrib import admin
from .settings import MEDIA_ROOT
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^', include('blog.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
)

Including Django REST Framework urls returns 404

I get a 404 showing the following url patterns when I try to browse to http://localhost:8000/api/v1/api-dev/:
^api/v1/ ^api-dev/ ^login/$ [name='login']
^api/v1/ ^api-dev/ ^logout/$ [name='logout']
What's up with the whitespace and ^?
Here's the relevant code from the app.urls:
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
url(r'^api/v1/', include('api.urls')),
)
And from the api.urls:
from django.conf.urls import patterns, url, include
from rest_framework import routers
from api.views import views
urlpatterns = patterns('',
url(r'^api-dev/', include('rest_framework.urls', namespace='rest_framework')),
)
Oh, the answer is just that I'm an idiot... there is no
http://localhost:8000/api/v1/api-dev/
Only http://localhost:8000/api/v1/api-dev/login
Try removing the url part from both:
(r'^api/v1/', include('api.urls')),
(r'^api-dev/', include('rest_framework.urls', namespace='rest_framework')),

In Django, How do you write the url pattern for '/' and other root-based urls

I'm new to django, and one of the things that I'm still learning is url_patterns. I set up a pages app to handle the root path (http://www.mysite.com) as well as some static pages such as the about page. I figured out how to set up the url pattern for the root path, but I can't get the site to direct the path '/about' to the pages "about" view.
Here is my main urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^$', 'pages.views.root'),
url(r'^/', include('pages.urls')),
)
here is my pages urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('pages.views',
url(r'^about', 'about'),
)
Here is my pages views.py
# Create your views here.
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
def root(request):
return render_to_response('pages/root.html',context_instance=RequestContext(request))
def about(request):
return render_to_response('pages/about.html',context_instance=RequestContext(request))
If I change the main urls.py file to have r'^a/', include('pages.urls') then the path '/a/about' directs to the about action.. so I think it has to be an issue in the way i'm writing the url pattern in this file. But, I can't figure out how to change it. Can anyone help?
Figured out what the issue is. The proper url_pattern on the project level is:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^$', 'pages.views.root'),
url(r'', include('pages.urls')),
)
When this is in place, '/about' and other simple paths direct properly.
Thanks everyone!
Try this, for url.py on the project level:
urlpatterns = patterns('',
# Examples:
url(r'^$', 'apps_name.views.home', name='home'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^about/', include('about.urls')),
)
and then the url.py for the app about
urlpatterns = patterns('',
url(r'^$', direct_to_template, {"template": "about/about.html"}, name="about"),
)
Take into account that regular expression are evaluated from top to bottom, then if the path fits the regexp it will enter. To learn more about regexp google it or try the great book from Zed Shaw about regexps
Note that from Django version 2.0 the URL pattern has changed to use django.urls.path() Check Example here: link
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
About the url method:
url(r'^$', 'pages.views.root')
url is deprecated in Django 3.1, it's good to use re_path instead.
https://docs.djangoproject.com/en/3.1/ref/urls/#s-url
https://docs.djangoproject.com/en/3.1/ref/urls/#re-path
Note: The r'^$'pattern WON'T work with path function, and will give you a misleading error that the route could not be found.
You have to use re_path(r'^$', [etc]) every time you use a regular expression instead of a simple string in pattern.

Django application urls not working

In one application's urls.py I have:
urlpatterns = patterns('app.views',
url(r'^products/$', products, name="products"),
url(r'^$', index, name="index"),
)
In base project urls.py I have:
urlpatterns = patterns('',
(r'^$', include('app.urls')),
(r'^admin/', include(admin.site.urls)),
)
Why http://127.0.0.1:8000/ - works fine with app.views.index method
while http://127.0.0.1:8000/products/ - returns 404 error and is not defined in url routes?
Spent some time on it already and can't find solution, maybe there is something simple that I miss...
Your base urls should be:
urlpatterns = patterns('',
(r'^', include('app.urls')),
(r'^admin/', include(admin.site.urls)),
)
The '$' is only used for urls. If you look at the doc, it will tell you not use the '$' when using include().
urlpatterns = patterns('',
(r'^', include('app.urls')),
(r'^admin/', include(admin.site.urls)),
)
worked fine.
I was having the same issue while using path() in Django URLs.
The simple fix is you don't have to use the slash at the end of the path otherwise Django will take that URL as a complete URL and will not go to the next urls.py file
//this will not work
path('/', include('app.urls'), name='profile_page')
// but this will work
path('', include('app.urls'), name='profile_page')

Why Django automatically adds slash after url that ends with ".htm" and doesn't when url ends with ".html"?

I have a problem that Django automatically adds slash to urls that ends with ".htm"
Url like:
http://127.0.0.1:8080/js/tiny_mce/themes/advanced/link.htm
becomes:
http://127.0.0.1:8080/js/tiny_mce/themes/advanced/link.htm/
But if I rename "link.htm" to "link.html" then no problem happens.
Where could be the issues?
Thanks.
urls.py:
from django.conf.urls.defaults import *
from dtunes.views import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', home, name='home'),
url(r'^(?P<path>.*\.(htm|html|jpg|jpeg|css|gif|js|png))$', "django.views.static.serve", {
"document_root": settings.MEDIA_ROOT,
}, name="media"),
url(r'^img/tr.gif', track, name='track'),
(r'^admin/', include(admin.site.urls)),
url(r'^smscoin/ipn/', ipn, name='smscoin_ipn'),
url(r'^download-link/', get_download_link, name='get_download_link'),
url(r'^get/(?P<name>.*)/$', item_details, name="item_details"),
url(r'^getnow', item_details_paid, name="item_details_paid"),
url(r'^download/(?P<name>.*)/$', send_direct_file, name="send_direct_file"),
url(r'^(?P<name>.*)/$', plain_page, name="plain_page"),
)
Django has a setting, "APPEND_SLASH", which adds a slash to URLs that are not otherwise matched in the URLConf, but would be matched if a slash were added. So you probably have some regex pattern in your urls.py that is matching ".htm/".
It looks like you are using Django to serve static files? If so, you might make sure this is configured properly. During development, to keep things DRY, I usually use the following in my "urls.py" file to serve static media. This requires a properly configured MEDIA_ROOT and MEDIA_URL in settings.py:
# urls.py
from django.conf import settings
urlpatterns = patterns(
...
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^%s/(?P<path>.*)$' % settings.MEDIA_URL[1:-1],
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)