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
Related
I am writing basic views and then putting it in urls.py but it is still not working.
views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("hello how are you")
project urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('blog.urls')),
]
blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/',views.home,name='home')
]
I have also included my app in settings of project (Installed apps).
Some thoughts about that:
Change your urls patterns in your apps urls to:
from .views import *
urlpatterns= [
path('', home,name='home')
]
If its not working send us your installed apps settings and wsgi file
You confused, you need to write http://127.0.0.1:8000/home not only http://127.0.0.1:8000 with your current code.
If you want the view to be rendered in default route so change urls.py as:
from . import views
urlpatterns= [
path('', views.home,name='home')
]
Otherwise, you can only append /home with your current code to work.
for the same code above it is working for the URL http://127.0.0.1:8000/home/
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
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 am working on a django project, but it returns
the included urlconf "myapp.urls"does not appear to have any patterns in it.
I tried checking my views to ensure I imported everything correctly
from django.contrib import admin
from django.urls import path
from .views import home
from accounts.views import login_view
urlpatterns = [
path('admin/', admin.site.urls),
path('',home),
path('accounts/login/', login_view),
]
I expect the site to run and redirect me to the login page
This is my views in the same directory with the urls.py
#login_required
def home(request):
return render(request,"home.html")
Your code doesn't have a problem, send the repository link
Include this In your settings.py
settings.py
LOGIN_URL = '/accounts/login/'
LOGOUT_URL = "/accounts/logout/"
LOGIN_REDIRECT_URL = " "
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')),