enter image description hereenter image description herei get the error of Template does not exist # [[e](https://i.stack.imgur.com/gSKFa.png)](https://i.stack.imgur.com/795HN.png)
this is my urls
when i run i get the error emplateDoesNotExist at / customer/index.html Request Method
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from customer.views import Index, About
urlpatterns = [
path('admin/', admin.site.urls),
path('', Index.as_view(), name='index'),
path('about/', About.as_view(), name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Your Templates settings in settings.py should look like this
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
And your urls.py in your deliver1 folder should look like this
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("the-name-of-your-app.urls")),
]
I don't have a lot of experience with class-based views. However you can try this to see if works for you
def index(request):
return render(request, "customer/index.html", {})
Your urls.py in your customer folder will look like this
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
so any error you encouter, let me know
Related
I have, Page not found (404) when accessing /category/Django
urls.py
from .views import (
PostListViewHome,
CategoryView,
)
from . import views
urlpatterns = [
path("", PostListViewHome.as_view(), name="Blog-home"),
path("category/<str:cats>/", views.CategoryView, name="category"),
]
views.py
def CategoryView(request, cats):
category_posts = Post.objects.filter(category=cats)
return render(request, 'Blog/category.html', {'cats':cats, 'category_posts':category_posts})
urls.py in the MPIE02
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.views.generic.base import TemplateView
from users import views as user_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='Logout'),
path('', include ('Blog.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if i try to accessing /category/ it showin me just 404
You are getting this error because in your settings file, there is no any path added of templates.
Add path in settings file:
import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'appname','templates',)],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I'm new in Django.
i am following a tutorial and now i have a problem .
my directory tree is like
and in /pages/views i have this
from django.http import HttpResponse
from django.shortcuts import render
def home_view(*args, **kwargs):
return "<h1>Hello world</h1>"
and in trydjango/urls i have this
from django.contrib import admin
from django.urls import path
from pages.views import home_view
urlpatterns = [
path('', home_view, name='home'),
path('admin/', admin.site.urls),
]
***but it can't recognize pages ***
i also tried these
solution
but didn't work!what do i missing?
even i added these codes to settings.py
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(BASE_DIR, 'pages'))
{ django 3.1 }
It looks like you forgot to create a urls.py file inside the 'pages' app.
in trydjango/urls.py you have to include the urls of the app:
urlpatterns = [
path('pages/', include('pages.urls'), name="pages"),
path('admin/', admin.site.urls),
]
Then create the pages/urls.py file and it should work:
urlpatterns = [
path('', home_view, name='home'),
]
note; dont forget to add 'pages' in the installed_apps inside your settings.py file.
Right now what I am doing is this:
pages app > urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
project > urls.py
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('pages.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
This seems to work fine. I am able to go to the homepage and the about page fine, but I have seen other people do as in the following:
project > urls.py
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('pages.urls')),
path('about/', include('pages.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
So basically, in the project urls file, the line
'path('about/', include('pages.urls')),'
is added as well as keeping the pages url the same as above.
So I was wondering what is the correct way of linking the about page in the project urls file.
If you have more than a single view and a single url for it in your app and you want to put all of them after some url prefix you put that prefix in urls.py root.
So lets say you have products and categories views and you want them behind api prefix. That means /api/products/ and /api/categories/ then you would have in your "api" app:
from django.urls import path
from . import views
urlpatterns = [
path('products', views.products, name='products'),
path('categories', views.categories, name='categories'),
]
In your root urls.py:
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('api/', include('api.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
in pages app you have to create urls.py file and link the url to the view
urlpatterns = [
path('about/', views.about, name='about'),]
and in templates you can call the url with its name
I would like to use the url from products app (products/urls.py) inside of search app url (search/urls.py) to search for items/products using search bar. I've attempted this example on django docs but it's importing a view to url in the same app, and I've also attempted this example but it looks to be a solution for an older version of django but i am using the latest version of django at time time 2.2.5.
The error message I am receiving in terminal is coming from search/urls.py:
path('', views.ProductListView.as_view(), name='list'),
AttributeError: module 'search.views' has no attribute
'ProductListView'
I understand search.views does not have an attribute "ProductListView" but, products.views does, which is why i'm trying to import products.views in search/urls.py.
products/urls.py
from django.urls import path, re_path
from .import views
app_name = "products"
urlpatterns = [
path('', views.ProductListView.as_view(), name='list'),
re_path(r'^products/(?P<slug>[\w-]+)/$', views.ProductDetailSlugView.as_view(), name='detail'),
]
search/urls.py
from django.urls import path
from .import views
from products.views import ProductListView
urlpatterns = [
path('', views.ProductListView.as_view(), name='list'),
]
ecommerce/urls.py (main app)
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include, re_path
# from products.views import ProductDetailView
from .views import home, about, contact
urlpatterns = [
path('admin/', admin.site.urls),
path('', home, name='home'),
path('about/', about, name='about'),
path('contact/', contact, name='contact'),
path('account/', include('allauth.urls'), name='login'),
path('register/', include('allauth.urls'), name='register'),
path('products/', include('products.urls', namespace='products')),
path('search/', include('search.urls', namespace='search')),
# path('', include('products.urls'), name='products-featured'),
# path('', include('products.urls'), name='featured-details'),
# path('', include('products.urls'), name='featured-slug-details'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You have:
from products.views import ProductListView
Therefore you should use ProductListView, not views.ProductListView
urlpatterns = [
path('', ProductListView.as_view(), name='list'),
...
]
Note you can remove the from .import views import unless you are using views somewhere else in search/urls.py
An alternative is to use import as, so that you can import multiple views.py from different apps in the same module:
from products import views as product_views
urlpatterns = [
path('', product_views.ProductListView.as_view(), name='list'),
]
I am trying to return different templates for different urls (having just one app), so basically I want to return:
One template for:http://127.0.0.1:8000/projects/
Another template for:http://127.0.0.1:8000/formpage/
I have the project urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from appone import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^projects/', include('appone.urls')),
url(r'^formpage/', include('appone.urls')),
]
And the app urls.py:
from django.conf.urls import url,include
from django.http import HttpResponse
from . import views
urlpatterns = [
url(r'^$', views.projs, name='proj'),
url(r'^$', views.form_view, name='form_view')
]
I have the views and templates , that are good, but I do not understand how can I return them based on the url, because for the moment I return the first view from app urls.py, for both urls.
Make separate view functions for both urls (projs and form_view).
Then in projects/urls.py
urlpatterns = [
...
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view')
...
]`
Or, if you want to have separate urls.py file
projects/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^', include('appone.urls')),
]`
appone/urls.py
urlpatterns = [
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view')
]`
Do you really need the second urls page? You could just set your first urls.py as:
from django.conf.urls import url,include
from django.contrib import admin
from appone import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view'),
]