These errors occur when you enter the admin page. I want to fix this. Help me.
problementer image description here
Source code
urls.py-fistsite
from django.contrib import admin
from django.urls import path, include
from polls import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', include('polls.urls')),
path('/admin', include('polls.urls'))
]
urls.py-polls
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/result/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote')
]
Change your urls.py to this and it should work.
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls), # add this
path('', views.index, name='index'),
]
For some reason you changed the site admin's url to include('polls.urls') which is incorrect. Change it back to path('admin/', admin.site.urls) and django will pick it up. And on the side note, You don't have to add / django has a middleware that does it automatically.
Related
urls.py
from django.urls import path from.import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
path('ourwork', views.ourwork, name='ourwork'),
path('portfolio', views.portfolio, name='portfolio'),
path('blog', views.blog, name='blog'),
path('careers', views.careers, name='careers'),
path('contact', views.contact, name='contact'),
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'artsoft/index.html')
def about(request):
return render(request,'artsoft/about.html')
def ourwork(request):
return render(request,'artsoft/ourwork.html')
def portfolio(request):
return render(request,'artsoft/portfolio.html')
def blog(request):
return render(request,'artsoft/blog.html')
def careers(request):
return render(request,'artsoft/careers.html')
def contact(request):
return render(request,'artsoft/contact.html') `
screen shot
The Error page
but when i clicking on blog this is work
Blog page
views.py
urls.py
directories of files
That because you /about/ has a slash at the end but /blog didn't. You could go with this:
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('ourwork/', views.ourwork, name='ourwork'),
path('portfolio/', views.portfolio, name='portfolio'),
path('blog/', views.blog, name='blog'),
path('careers/', views.careers, name='careers'),
path('contact/', views.contact, name='contact'),
and by default, Django has APPEND_SLASH=True, with this setting Django will add a slash at the end of your url so domain.com/blog and other paths which have no slash at the end also work as normal
As I can see where it worked (blog), you didn't add the last one /
One solution is
from django.urls import path from.import views
urlpatterns = [
path('/', views.index, name='index'),
path('about/', views.about, name='about'),
path('ourwork/', views.ourwork, name='ourwork'),
path('portfolio/', views.portfolio, name='portfolio'),
path('blog/', views.blog, name='blog'),
path('careers/', views.careers, name='careers'),
path('contact/', views.contact, name='contact'),
]
Or in the navigator put
127.0.0.1:8000/blog
127.0.0.1:8000/contact
127.0.0.1:8000/about
without the last /
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'),
]
In my django project, when I access localhost:8000 it says:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
The urls.py is:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace="polls")),
]
The polls urls.py is:
from django.conf.urls import url, include
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
url(r'^admin/', admin.site.urls),
]
The Django version is 1.10. Can anyone help me identify the problem? Thanks in advance.
You do not have a route for / it seems, so http://localhost:8000/ does not get you anywhere.
You wrote url(r'^polls/', include('polls.urls', namespace="polls")), in urls.py so all the routes defined in polls.urls are to be prefixed with polls/.
You may actually want to go to http://localhost:8000/polls/ (notice the polls/, because the route you defined as index is listed in the polls app.
Had you wanted to route your polls index to your url root, you should change urls.py to something like
urlpatterns = [
url(r'^', include('polls.urls', namespace="polls")),
url(r'^admin/', admin.site.urls),
]
and the forget about the polls/ part in the urls.
In main urls.py change
from
url(r'^polls/', include('polls.urls', namespace="polls")),
to
url(r'^$', include('poll.urls')),
I'm making the Django tutorial at the official website and I'm currently setting up URLs for the sample polling application we're creating.
As of now, my polls/urls.py looks like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.votes, name='vote')
]
And I can't help but notice the repetition of (?P<question_id>[0-9]+) so I wonder if there's a simpler way to avoid this besides extracting it to a constant like QUESTION_PATTERN = (?P<question_id>[0-9]+)
You can include a list of subpatterns, like this:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/', include([
url(r'^$', views.detail, name='detail'),
url(r'^results/$', views.results, name='results'),
url(r'^vote/$', views.votes, name='vote'),
]),
]