404 error Django - python

I'm beginner in Django programing, and I want to show a view on my browser.
My code is:
polls/view.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def animal(request):
return HttpResponse("Hello cat")
polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^animal$', views.animal, name='animal'),
)
When I show index view, everything in okay. But, when I show animal view, my browser not found the page, and I dont know why.
Thank you !

Your second URL pattern isn't terminated. It needs to be:
url(r'^animal/$', views.animal, name='animal')
also, you want to list your URLs from most specific to least specific. Also note that you can use the patterns prefix to keep your code a bit more DRY.
urlpatterns = patterns('views',
url(r'^animal/$', 'animal', name='animal'),
url(r'^$', 'index', name='index'),
)
as Django will try to match them top-down

Related

Having trouble URL mapping using Django for Python

I am having a trouble mapping urls correctly. I've included my code below.
I am able to run the code just fine, but when I click the "about" hyperlink, I get an error saying
The current URL, rango/about/, didn't match any of these.
When I put in the URL just "rango/", removing the "about", I get the following error:
The current URL, rango/, didn't match any of these.
I am a complete beginner with Django and have been going through the Tango with Django book, but currently stuck with the exercises on Ch3.
Any help is much appreciated. Thank you!
tango_with_django_project.urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from rango import views, urls
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^rango/',include('rango.urls')),
# above maps any URLs starting with rango/ to
# be handled by the rango application
url(r'^admin/', admin.site.urls),
]
rango.urls.py
from django.conf.urls import url
from rango import views
urlpatterns = [
url(r'^rango/', views.index, name='index'),
url(r'$^rango/about/',views.about,name='about'),
]
rango.views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hey there partner! \
<br/> <a href='/rango/about/'>about</a>")
def about(request):
return HttpResponse("Rango says here is the about page. \
<br/> <a href='/rango/'>index</a>")
# rango.urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about/$',views.about,name='about'),
]

Django redirecting everything to homepage

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!)

Django reverse lookup rendering the same (wrong) template

I have a small resume site that is not rendering a different template upon hitting a reversed URL.
site/scripts/templates/scripts/index.html:
<p>were at main</p>
python
<br/>
bash
These links 'python' and 'bash' work in the URL bar, they take us to localhost:scripts/bash/ and localhost:scripts/python/, but the exact same webpage is displayed (index.html, or localhost:scripts/)
site/scripts/urls.py:
from django.conf.urls import patterns, include, url
from scripts import views
urlpatterns = patterns('',
url(r'$', views.index, name='index'),
url(r'python/$', views.access_python, name='python'),
url(r'bash/$', views.access_bash, name='bash'),
)
site/scripts/views.py:
from django.shortcuts import render
def index(request):
return render(request, 'scripts/index.html')
def access_python(request):
return render(request, 'scripts/python.html')
def access_bash(request):
return render(request, 'scripts/bash.html')
site/urls.py (main folder w/ settings.py):
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'scripts/', include('scripts.urls', namespace='scripts')),
)
clicking 'bash' should retrieve:
site/scripts/templates/scripts/bash.html:
<p>we're at bash</p>
Why would a reverse lookup reach the correct URL, but not call the associated view that that URL pattern wants? Thank you
The index regex was catching any possible pattern, since it matched any end of string. I found out by moving the index pattern after the other 2. It should be:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^python/$', views.access_python, name='python'),
url(r'^bash/$', views.access_bash, name='bash'),
)
Any blank index urls (r'^$') need both start and end of string, to match the empty string after the first part of that pattern (in this case, 'scripts/')

Django - page not found 404

I'm trying to create a site using Django. I have the index view working, however I want to create a simple custom view and map to it but I am unable to. I'm getting a 404 error.
The app inside my project is called emails.
Here are the files:
base/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^emails/$', include('emails.urls')),
)
emails/urls.py
from django.conf.urls import patterns, include, url
from emails import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^custom/$', views.custom, name='custom'),
)
emails/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you're at the index.")
def custom(request):
return HttpResponse("This is a custom view.")
Here is the 404:
Using the URLconf defined in crm.urls, Django tried these URL patterns, in this order:
^admin/
^/emails/$
The current URL, emails/custom, didn't match any of these.
When I visit localhost:8000/emails/ - I see the index view. However localhost:8000/emails/custom/ is returning a 404 error. Any help would be appreciated!
This url(r'^emails/$', include('emails.urls')), should be without the dollar sign $:
url(r'^emails/', include('emails.urls')),
You don't want to end your path after emails, so don't end the string with the regex character $. Let it be able to be continued in the other urls.py file

how to display a view in Django?

I'm totally new to Django, and I'm trying to understand how does it work (I'm more used to PHP and Spring frameworks.
I have a project called testrun and inside it an app called graphs, so my views.py looks like:
#!/usr/bin/python
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You're at the graphs index.")
then, in graphs/urls.py:
from django.conf.urls import patterns, url, include
from graphs import views
urlpatterns = patterns(
url(r'^$', views.index, name='index'),
)
finally, at testrun/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testrun.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^graphs/', include('graphs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
However, when I try to access http://127.0.0.1:8000/graphs/ I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
What am I doing wrong that I can't get that simple message to be displayed in the browser?
To expand on my comment, the first argument to patterns() function is
a prefix to apply to each view function
You can find more information here:
https://docs.djangoproject.com/en/dev/topics/http/urls/#syntax-of-the-urlpatterns-variable
Therefore in graphs/urls.py you need to fix the patterns call like so:
urlpatterns = patterns('', # <-- note the `'',`
url(r'^$', views.index, name='index'),
)

Categories