Getting a 404 when trying to access a url python/django - python

Currently getting a 404 error for my application when I try to access the url /contato, and I am not sure why, can anyone help me?
Here is the error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8002/contato
(...)
^contato/
(...)
Here is my code.
My main urls.py has:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/password_reset/$', 'django.contrib.auth.views.password_reset', name='admin_password_reset'),
url(r'^admin/password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
url(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
url(r'^admin/', include(admin.site.urls)),
url(r'^perfil/', include('perfil.urls')),
url(r'^contato/', include('contato.urls')),
url(r'^$', 'views.index', name='index'),)
My contato.urls.py:
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('contato.views',
url(r'^contato/$', 'contato', name="contato"),
)
my views.py in contato:
from django.shortcuts import render_to_response
from django.template import RequestContext
def contato(request):
render_to_response('contato/contato.html',locals(),context_instance=RequestContext(request),)

You don't have a match for the url "/contato". In your base urls.py, you point the prefix "/contato" to include contato.urls, and then in that file you have a single URL which is again "/contato": so the combined URL for that view is "/contato/contato".
If you just want the URL to match "contato", you should either get the included url to match just "^$", or (probably better) don't bother including the separate urls.py and match the view directly from the base file.

Related

How to avoid Page 404 Error on my first app

I am following this tutorial.
https://www.youtube.com/watch?v=a48xeeo5Vnk
Here is my code for views:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse('<h1>This is our blog</h1>')
def about(request):
return HttpResponse('<h1>This is our About Page</h1>')
def next(request):
return HttpResponse('<h1>This is our next Page</h1>')
This is my App urls page code
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
path('next/', views.next, name='blog-NEXT'),
]
This is my main project URL code
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('firstapp', include('firstapp.urls')),
path('admin/', admin.site.urls),
]
Now when I try this with only single view i.e default page '', it works alone, with no more pages mentioned in code, however when I add more views it gives me a 404 page error. I believe the code is fine and it shoud work, but somehow it chose not to.
Tried different browser, tried forums but nothing.
Here is the error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/firstapp/
Using the URLconf defined in first.urls, Django tried these URL patterns, in this order:
firstapp [name='blog-home']
firstapp about/ [name='blog-about']
firstapp next/ [name='blog-NEXT']
admin/
The current path, firstapp/, didn't match any of these.
Any assistance would be appreciated.
Do below changes
from:
path('firstapp', include('firstapp.urls')),
to:
path('firstapp/', include('firstapp.urls')),

Still getting 404 page not found on Django using correct route files and settings

I'm going through the polls Django tutorial and have searched for answers to this. So far:
I made sure I'm using the right directories. The main urls.py is in mysite/mysite/, the polls urls.py is in mysite/polls/urls.py.
Tried adding 'polls' to INSTALLED_APPS in the settings.py of mysite/mysite.
Am making sure that I am requesting 127.0.0.1:8000/polls and not 127.0.0.1:8000
I am using Python 3.4 and Django 1.9, same as the tutorial.
I am still receiving this message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^%', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world")
In your polls/urls.py
change url(r'^%', views.index, name='index') to url(r'^$', views.index, name='index').
Also in your urls.py, you have route for 127.0.0.1:8000/polls/ and you request for 127.0.0.1:8000/polls . Notice the trailing slash.
So you should request to 127.0.0.1:8000/polls/ .
Add APPEND_SLASH = True in your settings.py file so that it would redirect 127.0.0.1:8000/polls to 127.0.0.1:8000/polls/ .
So i tried your Index view with your urls.py.
It works, the only thing different is in your "Request URL, you must request for - 127.0.0.1:8000/polls/%
changing your urls to
url('^/%', index) - polls URLs.py
url('^polls', include('polls.urls')) - project URLs
This will work.

Django reporting 404 error on simple view?

I'm just barely getting started with Python and Django.
I've created my modules, migrated, added my modules to the admin section, etc. Now, I need to create my first view. I've been following this guide (Yes the machine I'm on is running 1.6)
My views.py file in the app (setup) looks like this...
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Setup Index")
My app'a urls.py looks like this...
from django.conf.urls import patterns, url
from setup import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
And my root urls.py looks like this...
from django.views.generic import TemplateView
from django.views.generic.base import RedirectView
from django.conf.urls import include, patterns, url
from django.contrib import admin as djangoAdmin
from admin.controller import ReportWizard
from admin.forms import reportDetailsForm, reportDataViewsForm
from setup import views
djangoAdmin.autodiscover()
urlpatterns = patterns('',
url(r'^django-admin/', include(djangoAdmin.site.urls)),
#New User Wizard URL
url(r'^setup/', include('setup.urls')),
)
As far as I can tell I have followed the guide to the letter, but I am recieving a 404 error when navigating to myIP/setup.
There are no error messages on the 404 page. What is the issue?
For some reason your settings seem to override the default for either APPEND_SLASH or MIDDLEWARE_CLASSES.
Opening http://example.com/setup should cause a redirect to http://example.com/setup/, but somehow it doesn't for you.
Note that the latter URL is matched by your urls.py while the former is not (as should be the case).
The above should work if 'CommonMiddleWareis enabled andAPPEND_SLASHis set toTrue`.

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

Django url dispatcher - wrong function

i am new to Django and i have some problem with Django URL dispatcher.
I have "prometfire" project and "homepage" app.
My goal is to connect this paths to their view functions:
127.0.0.1:8000 --> "homepage_view"
127.0.0.1:8000/welcome --> "welcome_view"
"homepage_view" works fine, but when i go to 127.0.0.1:8000/welcome i have same result as in "homepage_view", instead of "welcome_view" result.
Am i missing something?
Django 1.5
Python 2.7
#urls.py in prometfire
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('homepage.urls')),
url(r'^welcome/', include('homepage.urls')),
url(r'^admin/', include(admin.site.urls)),
)
#urls.py in homepage app
from django.conf.urls import patterns, include, url
urlpatterns = patterns('homepage.views',
url(r'^$', 'homepage_view'),
url(r'^welcome/', 'welcome_view'),
)
#views.py in homepage app
from django.shortcuts import render_to_response
from django.http import HttpResponse
def homepage_view(request):
return render_to_response('homepage.html',
{'name': 'bob'}
)
def welcome_view(request):
return HttpResponse('Welcome')
Your problem is that you are including your homepage urls twice. Remove the second entry
url(r'^welcome/', include('homepage.urls')),
This is explained in the docs on including other url confs
Whenever Django encounters include() (django.conf.urls.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.
In your case, the 'welcome/' is removed from the url, which leaves '', which is matched by the url pattern for the homepage.
That's because it never enters the second condition for the app, it verifies the condition at the url root conf, welcome/, and after that goes directly to ^$ in the app. A solution would be remove the welcome/ from the url root.
The first welcome definition is redundant and is causing the "bug".

Categories