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)
]
Related
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.
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'm creating a basic app in my Django project. I mapped the views to url. While running this project it is showing
404 page not found.
in urls.py
from django.urls import path
from . import views
urlpatterns=[
path(' ',views.index,name="index"),
]
in views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("hello world");
in project/urls.py
from django.urls import include,path
from django.contrib import admin
urlpatterns = [
path("myapp/ ",include('myapp.urls')),
path('admin/', admin.site.urls),
]
I expect the output to be like hello world
Remove space character form your patters
In urls.py change
path(' ',views.index,name="index") to path('',views.index,name="index")
In project/urls.py change
path("myapp/ ",include('myapp.urls')), to path("myapp/",include('myapp.urls')),
I am trying to show a blogpost on the site.
Below is details of urls and view file details but still it showing a 404 not found page
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="ShopHome"),
path("blogpost/", views.blogpost, name="blogpost")
]
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'blog/index.html')
def blogpost(request):
return render(request, 'blog/blogpost.html')
Showing:
404 not found page
you should include urls.py of your application to project urls.py
actually if your project name is "mysite" and your application name is "blogspot", you should include mysite/blogspot/urls.py on this file mysite/mysite/urls.py like this:
from django.urls import path, include
urlpatterns = [
path('', include('blogspot.urls')),
]
Actually, i forget to add the slash at the time of registering the url of this app in urls