Django URL not working - python

Going to localhost/app works, but localhost/app/upload does not. Am I doing something really obviously wrong? I've tried a few different methods following the documentation without luck. I get a 404 saying the URL pattern does not match.
/project/app/urls.py
from app import views
urlpatterns = patterns('',
url(r'^$', views.view, name='result'),
url(r'^upload/$', views.upload, name='upload'),
)
/project/urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^app/$', include('app.urls')),
)

Don't terminate the inclusion URL.
url(r'^app/', include('app.urls')),

Related

Django url patterns, redirect from root to other url

This should be an easy enough problem to solve for you guys:
I just started working with Django, and I'm doing some routing. This is my urls.py in the root of the project:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls')),
]
This is the routing in my dashboard app:
urlpatterns = [
path('dashboard', views.index, name='index'),
path('', views.index, name='index'),
]
Now let's say I want my users to be redirected to /dashboard if they go to the root of the website. So I would use '' as a route in the urls.py in the root, and then have everyone sent to /dashboard from the urls.py in the dashboard app. But when I do this I get the following warning:
?: (urls.W002) Your URL pattern '/dashboard' [name='index'] has a route beginning with a '/'. Remove this slash as it is unnecessary. If this pattern is targeted in an include(), ensure the include() pattern has a trailing '/'.
So I tried to use '/' instead of '', but since a trailing / is automatically removed from an url, the url wouldn't match the pattern. Should I ignore/mute this warning or is there another way to go about it?
This is the code that worked perfectly but gave me a warning earlier:
urlpatterns = [
path('/dashboard', views.index, name='index'),
path('', views.index, name='index'),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls'))
]
You can use RedirectView to redirect from / to /dashboard/. Then use 'dashboard' when including the dashboard urls.
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name='dashboard:index')
path('dashboard/', include('dashboard.urls')),
]
You can then remove 'dashboard' from the path in dashboard/urls.py, as it is already in the include().
app_name = 'dashboard'
urlpatterns = [
path('', views.index, name='index'),
]
I've added app_name='dashboard' to match the namespace used above in pattern_name='dashboard:index'.
Note that Django projects usually use URLs with a trailing slash, e.g. /dashboard/ instead of dashboard.
If you really want to use URLs like /dashboard without a trailing slash, then the include should be
path('dashboard', include('dashboard.urls')),
If you do this, I suggest you set APPEND_SLASH to False in your settings.
You can try something like this:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import RedirectView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contacts/', include('appname.contacts.urls')),
url(r'^comments/', include('appname.urls')),
url(r'^subscriptions/', include('appname.partner.urls')),
url(r'^', RedirectView.as_view(url="/admin/"))
]
This is what I've done in my project so whenever the user go to 127.0.0.1:8000 it redirects to /admin

Page not found with Django (404)

In my django project, when I access localhost:8000 it says:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
The urls.py is:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace="polls")),
]
The polls urls.py is:
from django.conf.urls import url, include
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
url(r'^admin/', admin.site.urls),
]
The Django version is 1.10. Can anyone help me identify the problem? Thanks in advance.
You do not have a route for / it seems, so http://localhost:8000/ does not get you anywhere.
You wrote url(r'^polls/', include('polls.urls', namespace="polls")), in urls.py so all the routes defined in polls.urls are to be prefixed with polls/.
You may actually want to go to http://localhost:8000/polls/ (notice the polls/, because the route you defined as index is listed in the polls app.
Had you wanted to route your polls index to your url root, you should change urls.py to something like
urlpatterns = [
url(r'^', include('polls.urls', namespace="polls")),
url(r'^admin/', admin.site.urls),
]
and the forget about the polls/ part in the urls.
In main urls.py change
from
url(r'^polls/', include('polls.urls', namespace="polls")),
to
url(r'^$', include('poll.urls')),

Python Page 404 not found

New to Python, just starting up with a Issue reporting app which involves an API module.
My Urls.py file:
urlpatterns = patterns('',
url(r'^api/', include('api.urls')),
)
My api.urls file
urlpatterns = patterns('api.v1',
# Examples:
# url(r'^$', 'newproject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
# authentication / session managemen
url(r'^auth/profile/me/$', 'account', name='my-account'),
url(r'^auth/profile/$', 'new_account', name='new-account'),
url(r'^auth/session/(?P<key>[a-z0-9]{64})/$', 'session', name='existing-session'),
url(r'^auth/session/$', 'new_session', name='new-session'),
....
My Web Page 404 Error
Using the URLconf defined in newproject.urls, Django tried these URL patterns, in this order:
^api/
The current URL, , didn't match any of these.
I might be overlooking a mistake I made...
Help Please?
Using Django 1.9.7
replace url(r'^api/', include('api.urls')),
with url(r'', include('api.urls')),

Django urls.py only one app

I just started playing with Django, I love it! I followed the tutorial from the Django documentation, but have the following question:
I only have one app (polls), currently I always have localhost/polls/{urlname}
Is there a way to remove the polls keyword? So people that go to localhost automatically go to my app polls? At the moment, I have this wildcard
url(r'.*$', RedirectView.as_view(url='polls/', permanent=False), name='index'),
But this still keeps polls in the url. This is my complete urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'.*$', RedirectView.as_view(url='polls/', permanent=False), name='index'),
)
Thanks in advance!
Just remove the polls/ prefix in the regex of the "include" url:
urlpatterns = patterns('',
url(r'^', include('polls.urls', namespace="polls")),
...
)

Django urls.py bug - some patterns not working

I've encountered a one nasty bug in my code while developing a personal blog in Django. Basically, I've changed my urls.py file by adding a couple of rules to make certain views accessible.
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from blog import views
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
]
Everything seems to be working except when I try access http://127.0.0.1:8000/about or /posts, Django throws out a 404 error. What is the reason of this? I've defined both rules but the system seems not to recognize the pattern - maybe I've mispelled something? Maybe I know nothing about url formatting (could be, it's my first time doing this stuff)?
A big thanks from a newbie programmer to everyone who finds the bug :)
The url-patterns are processed from top to bottom. Your third pattern ^(?P<slug>\w+) consumes everything, so about and posts is never reached.
An example: Django wants to find the view for the url about/. The patterns ^admin/ and ^$ do not match. But ^(?P<slug>\w+) does, because about starts with letters or numbers (the character sets contained in \w)
>>> import re
>>> re.search('^(?P<slug>\w+)', 'about/')
<_sre.SRE_Match object at 0x10b5b7be8>
So Django found a match, callBs views.view_post and finishes the request. That means, the more specific rule must come first. Better: avoid ambiguity.
You have to change position of url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),.
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
]
urlpatterns is list and order of urls is important.

Categories