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.
Related
I followed a tutorial by a youtuber known as Mosh, I followed along with his Django tutorial but whenever I enter the URL pattern, it gives me error 404, page not found. This is a screen shot of the explorer tab.
I only edited the views.py, products.urls.py and pyshop.urls.py files.
views.py:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse("Welcome to the new products page!")
def new(reqest):
return HttpResponse("New Products")
products.urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index),
path("new", views.new)
]
pyshop.urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("products/", include("products.urls"))
]
the way you have it should work if you have the url as
localhost:8000/products
#or
localhost:8000/products/new
these are my main codes i wrote to create wepapp but i get this 404 error so pleas help
my hello urls.py
from django.urls import path
from hello import views
urlpatterns = [
path("", views.index, name="index")
]
my urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls")),
]
my views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("hello, orld")
It looks like you are trying to access index with
http://127.0.0.1:8000/. This will not work, because your index is included in hello's urls.py, and hello's url starts with hello/.
Try
http://127.0.0.1:8000/hello
instead.
I am writing a code to map a url '/products/new' to its view function 'new'. Instead of getting an HttpResponse from 'new' I get HttpResponse of 'index' function which is also inside views.py
My views.py code inside products.py is --
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('This is home page')
def new(request):
return HttpResponse('New Products')
My urls.py code inside products.py is --
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^', views.index),
url(r'^new', views.new)
]
My urls.py file inside the project file is --
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns=[
url(r'^admin/', admin.site.urls),
url(r'^products/', include('products.urls'))
]
Now when i access 127.0.0.1:8000/products I get desired result that is "This is the home page"
But when i access 127.0.0.1:8000/products/new or even 127.0.0.1:8000/products/new/xxx/xxx any random text after /products i get the same result that is "this is home page"
I don't know what is happening. Please help.
Add this url pattern in your project urls.py
url(r'^products/(?P<new>)/$', views.new),
#alasdair Thank you.
Adding the dollar to r'^new/$' is correct, because it stops it from matching new/something/else. But it's adding the dollar to ^$ that fixed the problem, by stopping it from matching new
So the corrected code in urls.py of products app is -
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$', views.index),
url(r'^new/$', views.new)
]
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),
I am doing a Django course and in my first tutorial only I am getting "Page on found 404 error" while mapping the URL will the view.
I referred to this link and did same steps I am still getting same error.
https://docs.djangoproject.com/en/3.0/intro/tutorial01/
polls/urls.py
-------------
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
polls/view.py
-------------
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
mysite/urls.py
--------------
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Directory of my code
Help me with the error... I checked all sources and followed exactly the link... Still cannot run it properly