i have created a view in views.py and when i try to add the url i get the error. I am following a tutorial and doing exactly as told.created view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
adding url
from first_app import views
urlpatterns = [
url(r'^$',views.index,name='index'),
path('admin/', admin.site.urls),
]
tutorial im following
You need to import url so that you can use it in your urls.py
from django.urls import path
from django.conf.urls import url
from first_app import views
urlpatterns = [
url(r'^$',views.index,name='index'),
path('admin/', admin.site.urls),
]
You should be aware that url is the old way of defining url patterns and is likely to be deprecated in the future
Related
hey i have a url path that i want to lead me to an application url file
but it says page not found
here is my core url:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls', namespace='store')),
path('basket/', include('basket.urls', namespace='basket')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and here is my basket.url file:
from django.urls import path
from . import views
app_name = 'basket'
urlpatterns = [
path('', views.basket_summary, name='basket_summary'),
]
and this is the view:
from django.shortcuts import render
# Create your views here.
def basket_summary(request):
return render(request, 'store/basket/summary.html')
my app name is basket , and i might add all of the files are defined.
whats the problem ?
You should add a trailing slash after /basket/.
Read more about APPEND_SLASH in the official documentation.
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
Using the URLconf defined in blogs.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
this is the error i am getting after i wrote the following code:
for urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
for polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
3.for polls/views.py
"""
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
"""
how i can i remove this error in vs code
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 doing tutorial "Movie rental" and i got error.
Using the URLconf defined in vidly.urls, Django tried these URL patterns, in this order:
Using the URLconf defined , Django tried these URL patterns, in this order:
1.admin/
2.movies/ [name='movie_index']
3.movies/ <int:movie_id [name='movie_detail']
The current path, movies/1, didn't match any of these.
my code is(from main urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movies/', include('movies.urls'))
]
from mysite i.e movies(urls.py)
from . import views
from django.urls import path
urlpatterns = [
path('', views.index, name='movie_index'),
path('<int:movie_id', views.detail, name='movie_detail')
]
from views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Movie
def index(request):
movies = Movie.objects.all()
return render(request, 'movies/index.html', {'movies': movies})
def detail(request, movie_id):
return HttpResponse(movie_id)
What i'm doing wrong?
In your movies.urls you forgot to close URL parameter.
path('<int:movie_id>/', views.detail, name='movie_detail')
Instead you did,
path('<int:movie_id', views.detail, name='movie_detail')
You're missing a closing >/.
path('<int:movie_id>/', views.detail, name='movie_detail')