Trying to reach url with Path include() shows 404 in Django - python

first of I want to apologize if I use the wrong terms or words in my question. I'm completely new to Django and got only a few months of experience with python. I hope you can understand my question anyways. I also want to acknowledge the fact that I'm using some imports that are not needed here and might not be relevant to the latest version of Django, I'm starting to get lost in all the things I've tried from other threads to solve my problem.
I'm having some problems with showing a page from apps url.
I'm getting redirected to my homepage when trying to reach localhost:8000/articles (because /articles gives 404 error)
I'm not sure exactly what code I need to include here, so bear with me.
articles/urls.py and articles/views.py
from django.conf.urls import url
from django.urls import include, path
from django.conf.urls import include, url
from django.urls import path
from .import views
urlpatterns = [
path('^$', views.article_list),
]
from django.shortcuts import render
from django.http import HttpResponse
# views
def article_list(request):
return render(request, "articles/article_list.html")
The project's urls.py and project's views.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from django.urls import include, path
from django.conf.urls import include, url
from django.urls import path, re_path
from .import views
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
path('about/', views.about),
re_path('^.*$', views.homepage)
]
from django.http import HttpResponse
from django.shortcuts import render
#Views
def homepage(request):
# return HttpResponse('homepage')
return render(request, "homepage.html")
def about(request):
# return HttpResponse('about')
return render(request, "about.html")
Im getting no errors or such.
So, my question is - does anybody have a clue why /articles generate 404 error?
Thank you in advance.

Firstly, don't use ^$ with path(). You only use regular expressions with re_path.
path('', views.article_list),
Usually, /articles will be redirected to /articles/ with a trailing slash.
However, in your case, you have a catch-all pattern:
re_path('^.*$', views.homepage)
This matches /articles, so you see the home page. Note it's not redirected as you say in your answer, the browser bar will still show /articles.
Unless you have a really good reason to have the catch all, I suggest you remove it and change it to
re_path('^$', views.homepage),
or
path('', views.homepage),
That way, you'll see the homepage for localhost:8000, localhost:8000/articles will be redirected to localhost:8000/articles/, and you'll get a 404 for pages that don't exist, e.g. localhost:8000/art/

Just using a empty string '' instead of '^$:
urlpatterns = [
path('', views.article_list),
]
Take a look at the last example here: https://docs.djangoproject.com/en/3.1/topics/http/urls/#url-namespaces-and-included-urlconfs
*I don't know what django version are you using, but for regular expressions paths you should use re_path() https://docs.djangoproject.com/en/3.1/ref/urls/#django.urls.re_path

Related

Django Tutorial. Page not found

I already search for any answer which could help me before write this question, but I haven't found anything that helps.
The thing is that I follow the tutorial and I can't see the view that I created.
Now I'm going to share my code:
project urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls urls.py:
from django.urls import path
from . import views
urlpatterns = [
path(" ", views.index, name='index'),
#127.0.0.1/polls/
]
polls views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
HttpResponse("Welcome to de Polls Universe Index")
OK I ALREADY KNOW WHATS GOING ON:
I forgot the RETURN before de HttpResponse.
There are two issues with your code:
The path for the index view contains a blank, which must be removed
The view function must return a response object. Please add return in front of the last line.

Django 404 error even though I've created the path and view

I'm just beginning to learn Django.
I've created a simple web sub-app called 'flavo' inside of another one called 'djangoTest'
When I run http://127.0.0.1:8000/flavo
it correctly displays
Hello, World!
then when I run http://127.0.0.1:8000/flavo/a it should show
Hello, a!
But instead I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/flavo/a
Using the URLconf defined in testDjango.urls, Django tried these URL patterns, in this order:
admin/
flavo [name='index0']
flavo a [name='a']
The current path, flavo/a, didn’t match any of these.
in testDjango/hello/views.py I have
from django.http import HttpResponse
from django.shortcuts import render
def index0(request):
return HttpResponse("Hello, world!")
def a(request):
return HttpResponse("Hello, a!")
In testDjango/flavo/url/py I have
from django.urls import path
from . import views
urlpatterns = [
path("", views.index0, name="index0"),
path("a", views.a, name="a"),
]
The only other file I've changed is , testDjango/testDjango/urls.py"
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('flavo', include("flavo.urls")),
]
I'm confused why I can't access http://127.0.0.1:8000/flavo/a
Add '/'.
like this.
# testDjango/testDjango/urls.py
path('flavo/', include("flavo.urls"))

Page not found at /polls

I am a total beginner in "django" so I'm following some tutorials currently I' am watching https://youtu.be/JT80XhYJdBw Clever Programmer's tutorial which he follows django tutorial
Everything was cool until making a polls url
Code of views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
HttpResponse("Hello World.You're at the polls index")
Code of polls\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Code of Mypr\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('polls/',include('polls.urls')),
]
I don't get it I did the same thing but I'm getting error not only polls.In one turtorial he decided to make blog,and again the same error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in
this order:
admin/
The current path, polls/, didn't match any of these.
Please my seniors help me.
Note:I'm using the latest versions of django,windows and as editor I'm using Pycharm.
Already tried(and did not work):
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, 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.
try to access polls/ URL in your browser then you will access the page
Because you have accessed the URL of your project, you have to go to this URL to access your app
try by changing your code like this
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
I was facing the same error. Feel kinda dumb after figuring out how to solve this.
Your urls.py content is all right
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But it must be included in the mysite\mysite\urls.py and not in mysite\urls.py.
That means the inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.

Django Tutorial Constant 404 error urlpattern recognition

I have searched endlessly for a solution and I can't seem to find it, even though I know this has been asked before. I am encountering a perpetual 404 error from the django tutorial. It appears to me that the 'urlpatterns' isn't recognizing another option of than admin.
backend/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('test/', include('meat.urls')),
path('admin/', include(admin.site.urls)),
]
backend/meat/urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
backend/meat/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(requests):
return HttpResponse('Hello Wold')
In order to access views inside the meat app.
you have included the urls of meat to the test/ path.
Thus you should use url /test/
if you are developing on local host then localhost:8000/test/
NB: I have tested this right now link
PS: try changing the path of admin site to path('admin/', admin.site.urls),

Can't get url request to show, 404

I'm completely new to backend, working through the djangobook tutorial. If I'm missing any vital information, let me know. The first task is to get 'Hello World' to show up on your development server, and it keeps returning 404. The two files in question being the views.py (my hello world file) and urls.py
this is the views.py:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
this is the urls.py:
from django.conf.urls import url
from django.contrib import admin
from mysite.views import hello
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', hello),
]
I feel like its not finding the views file correctly? This is how there set up, exactly as he said to do it in the tutorial
You need to change the URL's in two places. One in your app like you did, and the other in the django directory. This link has more information on how to do it.
from django.conf.urls import patterns, include, url
urlpatterns = patterns(
'',
url(r'', include('hello.urls')),
)
It was at http://127.0.0.1:8000/hello/, also used HunkDivine's answer as another option

Categories

Resources