I have completed all seven steps of the Writing your first Django app tutorial for 3.0. Everything works great, except the base site URL for 'mysite' at http://127.0.0.1:8000/. At the beginning of the tutorial, it worked, but it stopped working as I progressed/at the end. http://127.0.0.1:8000/admin/ works fine. Can anyone tell me what I'm doing wrong?
Based on how the tutorial is structured, i.e. "add this and that and this and that," etc., that I overwrote something.
Here is the error I'm receiving in my browser:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/
admin/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Here is the error I'm receiving in Terminal:
Not Found: /
[20/Jun/2020 11:01:22] "GET / HTTP/1.1" 404 2027
Here is my polls/urls.py file:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
Here is my mysite/urls.py file:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Here is TEMPLATES in mysite/settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
SOLVED/ANSWER added from comments:
Answer from expert Willem Van Onsem
1. Edit mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), # no /polls/ prefix
path('admin/', admin.site.urls),
]
2. Run python manage.py runserver
Can anyone tell me what I'm doing wrong?
All your paths for the poll app are here prefixed by polls/, since you wrote:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
You thus can visit the IndexView by fetching the http://127.0.0.1:8000/polls/ URI. If you do not want to prefix the urls, you can rewrite the root urls.py to:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), # no /polls/ prefix
path('admin/', admin.site.urls),
]
Then you can simply visit http://127.0.0.1:8000/.
Related
I've been hitting a snag with my Django app. Running Python manage.py runserver in my development environment, I get this error:
django.core.excptions.ImproperlyConfigured. The TEMPLATE_DIRS setting must be a list or a tuple. After changing TEMPLATE_DIRS to a tuple in settings.py as this TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) I got :
TypeError('Invalid path type: %s' % type(value).__name__)TypeError: Invalid path type: tuple.
I got a similar response when I changed it to a list.
Meanwhile, running Python manage.py runserver outside the development environment I got:
expected str, bytes or os.PathLike object, not tuple on both times I changed TEMPLATE_DIRS to a list and tuple.
I should also add that my template files aren't loading. I get:
Using the URLconf defined in myProject.urls, Django tried these URL
patterns, in this order:
accounts/
admin/
The current path, index/, didn't match any of these
Here is the project tree:
|_______myApp
|______ |______pycache__
| |______migrations
| |________init__
| |______admin
| |______apps
| |______models
| |______tests
| |______urls
| |______views
|______myProject
| |________init__
| |______asgi
| |______settings
| |______urls
| |______wsgi
|______env
|______static
|______templates
|______myApp
|______index.html
|______signup.html
manage.py
myProject/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('accounts/', include('accounts.urls')),
path('admin/', admin.site.urls),
]
myApp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('signup/', views.signup, name='signup'),
]
views.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('signup/', views.signup, name='signup'),
]
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates'), ]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIRS],
'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',
'django.template.context_processors.media',
], }, },]
I don't know how else to fix it.
TEMPLATE_DIRS is already a list, so it makes no sense to wrap that again in the list, you should define the TEMPLATES setting as:
TEMPLATES = [
{
# …,
'DIRS': TEMPLATE_DIRS,
# …
}
]
Starting development server at http://127.0.0.1:8000/
Not Found: /admin/
[30/May/2021 20:33:56] "GET /admin/ HTTP/1.1" 404 2097
project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('marketability.mkbl_urls')),
path('admin/', admin.site.urls),
path(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
variants path(r'admin/', admin.site.urls), and path(r'^admin/', admin.site.urls), don't works too.
project/marketability/mkbl_urls.py
from django.urls import path
from django.views.generic.base import RedirectView, TemplateView
from . import views
app_name = 'marketability'
handler404 = 'marketability.views.handler404'
urlpatterns = [
path('', views.home, name="home"),
path('<slug:cat_>/', views.page_Category_Main, name='cat'),
path('<slug:cat_>/<slug:product_>', views.page_Product, name='product'),
path('al_about.html', views.about, name="about"),
path('al_home.html', views.home, name="home"),
path('search_all.html', views.search_all, name="doorway"),
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain")),
path('sitemap.xml', TemplateView.as_view(template_name="sitemap.xml", content_type="text/xml")),
path('favicon.ico', RedirectView.as_view(url='/static/marketability/favicon.ico', permanent=True)),
]
project/marketability/admin.py
from django.contrib import admin
from .models import TxtHow, TxtRatings
# Register your models here.
admin.site.register(TxtHow)
admin.site.register(TxtRatings)
project/settings.py
....
NSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marketability',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/marketability/patterns'],
'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',
],
},
},
]
....
superuser has created
So, i don't see any deviations from Django Documentation.
How to solve it?
thks
Your path:
path('<slug:cat_>/', views.page_Category_Main, name='cat'),
will match with admin/ and thus see admin as the value for the cat_ slug, and thus trigger that view. Likely the view for that path will try to fetch an element with admin as slug, and fail to do this, and thus raise a HTTP 404 response.
You can put the urls for the admin/ and ckeditor/ before the one with your categories:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('', include('marketability.mkbl_urls')),
]
Django will visit the url patterns top to bottom, and thus trigger the first path pattern that matches, in that case the one with the admin.
It might however be better to prefix your marketability urls as well, for example with:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('market/', include('marketability.mkbl_urls')),
]
otherwise it is thus impossible to have a category named admin.
I have a Django projects with two apps, "projects" and "codebox", they were running fine, then at some point I got the following error:
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered
namespace
If I remove the link to my admin panel from my template this error goes away, but then I can't get to my admin panel:
Admin
I was working in the urls.py files when this error occurred, have I changed something that is inadvertently having an impact on the admin link?
Here is my top level urls.py file:
from django.contrib import admin
from django.urls import include, path, re_path
urlpatterns = [
# path('', include('projects.urls')),
path('projects/', include('projects.urls')),
re_path('^accounts/', include('django.contrib.auth.urls')),
re_path('^logged_out/', include('projects.urls')),
path('codebox/', include('codebox.urls')),
]
Here is my urls.py for "projects":
from django.urls import path, include
from . import views
app_name = 'projects'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('insight/', views.InsightView.as_view(), name='insight'),
path('logged_out/', views.LoggedoutView.as_view(), name='loggedout'),
path('insight/subgen/', views.SubgenView.as_view(), name='subgen'),
]
And here is urls.py for my second app, codebox:
from django.urls import path, include
from . import views
app_name = 'codebox'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('form/', views.CreateView, name="form"),
]
add this in top of project/urls.py
from django.conf.urls import include, url
then add this in the url_pattern
url(r' ', include(('<app_name>.urls','<app_name>'), namespace='<app_name>')),
hope this will work
Check your base html file which you're extending to other html pages. If there is some space between urls tag remove that space and save your file and try again you can check below code for ref.
from django.contrib import admin
from django.urls import path, include
from passapp import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('passapp/', include('passapp.urls')),
]
href="{% url 'admin:index' %}"
'Copied this code from the Django tutorial into my app's urls.py file...
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>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
When I start my server it produces the following error...
(urls.W005) URL namespace 'polls' isn't unique. You may not be able
to reverse all URLs in this namespace
I've tried using some other name besides 'polls' but with the same result. What am I doing wrong?
Check your root urls file, and be sure to have unique name:
(django 2+)
For example:
mysite/urls.py
-->
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('polls.urls')), #polls.urls is unique
path('admin/', admin.site.urls), #admin.site.urls is unique
]
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?P<question_id>[0-9a-f-]+)/$',views.detail,name='detail'),
url(r'^(?P<question_id>[0-9a-f-]+)/results/$',views.results,name='results'),
url(r'^(?P<question_id>[0-9a-f-]+)/vote/$',views.vote,name='vote'),
]
Write your url file like shown above.
I keep getting this error
Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name.
i am trying to use the default view from
from django.contrib.auth.views
from django.conf.urls import url
from accounts import views
from django.contrib.auth.views import (login,
logout,
password_reset,
password_reset_done,
password_reset_confirm,
)
urlpatterns =[
url(r'^$', views.cover, name='cover'),
url(r'^home/$', views.home, name = 'home'),
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"),
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"), # views define a link to connecct this to views then to template
url(r'^register/$', views.register, name="register"),
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^reset-password/$', password_reset, name= 'password_reset'),
url(r'^reset-password/done/$', password_reset_done, name='password_reset_done'),
url(r'^reset-password/confirm/$', password_reset_confirm, name='password_reset_confirm')
]
Please anybody help me out... i have check all... but couldnt find the fault.
it'll work if you just use path('', include('django.contrib.auth.urls')) in your main urls.py not the one in your app_name.
plz try this
be carefull to watch for some errors like not adding the $ at the end of some urls or maybe adding it. also pay attention to where success_url is given because the internal code uses it and is lost without it.
here in this code the application that I chose to manage users is called accounts, you can call yours anything.
the templates should be put inside a directory that is recognized by django otherwise it won't find them here is the code for settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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 here is the full urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from accounts.views import signup
urlpatterns = [
url(r'signup/$', signup, name='signup'),
url(r'login/$',auth_views.LoginView.as_view(template_name='login.html')),
url(r'logout/$',auth_views.LogoutView.as_view(template_name='logout.html')),
url(r'password_change/$',auth_views.PasswordChangeView.as_view(template_name='password_change.html',success_url='/accounts/password_change_done')),
url(r'password_change_done/',auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html')),
url(r'password_reset/$',auth_views.PasswordResetView.as_view(template_name='password_reset.html',email_template_name='password_reset_email.html',subject_template_name='password_reset_subject.txt',success_url='/accounts/password_reset_done/',from_email='support#yoursite.ma')),
url(r'password_reset_done/',auth_views.PasswordResetDoneView.as_view(template_name='password_reset_done.html')),
url(r'password_reset_confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html',success_url='/accounts/password_reset_complete/')),
url(r'password_reset_complete/',auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html')),
]
I had the same problem but solved it by adding the success_url parameter in PasswordResetView :
Add success_url parameter of Class Based View PasswordResetView. This will replace default route of password_reset_done
url(r'^reset/$',PasswordResetView.as_view(
template_name='password_reset.html',
email_template_name='password_reset_email.html',
subject_template_name='password_reset_subject.txt',
...
success_url = reverse_lazy('accounts:password_reset_done')
...
...
),name='password_reset'),
These occurs when Django is updated from 1.xx to 2.xx (well, as in my own case).
This is my solution. I fixed it by including a dictionary with key "post_change_redirect" with it's value pointing to the password_change_done url. For password resetting use post_rest_redirect.
from django.contrib.auth import views as v
from django.conf.urls import url
urlpatterns =
......
url(r"password-change/$", v.password_change, {"post_change_redirect":"account:password_change_done"}, name="password_change")
.........
]
This particular error may also occur when Django was upgraded from version 1.xx to version 2.xx
To fix this:
Add this line in main url.py:
url(r'^', include('django.contrib.auth.urls'))
And then replace functions with classes in 'your_app/urls.py'. E.g.
password_reset => PasswordResetView.as_view()
For example:
url(r'^reset-password/$', PasswordResetView.as_view(), name='password_reset'),
url(r'^reset-password/done/$', PasswordResetDoneView.as_view(), name='password_reset_done'),
url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
url(r'^reset-password/complete/$', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
I found this code and this seems to work.
from django.conf.urls import url
from accounts import views
from django.contrib.auth.views import (login,
logout,
password_reset,
password_reset_done,
password_reset_confirm,
)
from django.contrib.auth import views as auth_views
urlpatterns =[
url(r'^$', views.cover, name='cover'),
url(r'^home/$', views.home, name = 'home'),
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"),
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"),
url(r'^register/$', views.register, name="register"),
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'accounts/registration/password_reset_email.html',
'subject_template_name':'accounts/registration/password_reset_subject.txt',
'post_reset_redirect':'accounts:password_reset_done',
'from_email':'accounts#django.com',
},name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, {'template_name': 'accounts/registration/password_reset_done.html'}, name='password_reset_done'),
in above code accounts in myapp name. where you can put your own apps name
This is for Django 1.11
You are missing an import, app_name, some templates and template routes.
Change your code for this:
from django.conf.urls import url
from accounts import views
from django.contrib.auth import views as auth_views
app_name = 'accounts' # Django 2.0+, if not add namespace = 'accounts' on the urls.py where you are including this set of urls.
urlpatterns =[
url(r'^$', views.cover, name='cover'),
url(r'^home/$', views.home, name = 'home'),
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"),
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"),
url(r'^register/$', views.register, name="register"),
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^change-password/$', views.change_password, name='change_password'),
url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'registration/password_reset_email.html',
'subject_template_name':'registration/password_reset_subject.txt',
'post_reset_redirect':'accounts:password_reset_done',
'from_email':'accounts#django.com', # Yours
},name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, {'template_name': 'registration/password_reset_done.html'}, name='password_reset_done'),
]
Be sure in your folder "accounts" you have: /templates/registration/*.html with all your templates
For me the key info that was missing was the reverse resolve needing the name attribute set on the url entry!
path(
'password_change/done/',
auth_views.PasswordChangeDoneView.as_view(template_name='SyllabusTrackerApp/change-password-done.html'),
name="password_change_done"
),
The app_name namespace wasn't set either, but those weren't used in my setup for simplicity, so I could ignore that.
The most simple solution which I've found is as below, this may help.
for password reset link first import in urls.py file of your app.
from django.contrib.auth import views as auth_views
then add the following path in your app urls.py file
path('password_reset', auth_views.PasswordResetView, name='password_reset'),
finally you can use it in your HTML pages as below
<a href="{% url 'accounts:password_reset'%}" class="float-end">
I had same issue, I just added this to my main project url:
path('', include('django.contrib.auth.urls'))
Works like magic.