I am getting a duplicate url pattern from newly added app : http://127.0.0.1:8000/quote/quote/new/
base app urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
path('', include('post.urls')),
path('quote/', include('quote.urls')),
]
New app urls.py:
urlpatterns = [
path('quote/new/', QuoteCreateView.as_view(), name='quote-create'),
]
I have tried: path('', include('quote.urls')), in base urls.py but get the same problem.
In your "base app" urls.py you've got this line:
path('quote/', include('quote.urls')),.
In "new app" ("quote app") you've got this line:
path('quote/new/', QuoteCreateView.as_view(), name='quote-create'),
When you include 'quote.urls', each of the URLs is added to the quote/ prefix, which you've set in "base app". In you want your app to create URLs like quote/new, then you should do the following:
in "base app" urls.py:
path('quote/', include('quote.urls')),
in "new app" (or "quote app") urls.py:
path('new/', QuoteCreateView.as_view(), name='quote-create'),
remove the "quote/" in the url.py of your app. you already have a pre-fix in urls.py base app.
urlpatterns = [
path('new/', QuoteCreateView.as_view(), name='quote-create'),
]
base app urls.py:
urlpatterns = [ path('quote/', include('quote.urls')), ]
New app urls.py:
urlpatterns = [ path('new/', QuoteCreateView.as_view(), name='quote-create'), ]
Related
This is my main urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("shop.urls")),
]
I want that any url entered by user will redirect to shop.urls and find there
like if the user enters /index it will search index in shop.urls not in main urls.
My shop.urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('index', views.index),
]
You need to add / at the end of route so:
urlpatterns = [
path('', views.index),
path('index/', views.index),
]
Then enter the requested url as http://127.0.0.1:8000/index/
In main urls:
just give route name.
urlpatterns = [
path('shop/', include("shop.urls")),
]
And this is your shop urls:
urlpatterns = [
path('index/', views.index),
]
After changing above code.
You can navigate like this in your browser:
localhost:8000/shop/index/
You will redirect to index page.
urls.py
from django.urls import path
from app import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',views.ProductView.as_view(),name="home"),
path('product-detail/<int:pk>', views.ProductDetailView.as_view(), name='product-detail'),
path('cart/', views.add_to_cart, name='add-to-cart'),
path('buy/', views.buy_now, name='buy-now'),
path('profile/', views.profile, name='profile'),
path('address/', views.address, name='address'),
path('orders/', views.orders, name='orders'),
path('changepassword/', views.change_password, name='changepassword'),
path('mobile/<slug:data>', views.mobile, name='mobiledata'),
path('login/', views.login, name='login'),
path('registration/', views.customerregistration, name='customerregistration'),
path('checkout/', views.checkout, name='checkout'),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
you don't have a URL named 'mobile' you have a URL named 'mobiledata'.
you can change your URL name to 'mobile' or change it to 'mobiledata' wherever you are reversing.
I am having a problem with the password reset . Its works perfectly if i delete the namespace app_name = 'crm' . But when i include app_name = 'crm' i get the error,
Error: Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
But I want it to work without removing the namespace.
My urls.py
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
app_name = 'crm'
urlpatterns = [
path('', views.home, name='dashboard'),
path('login/', views.loginPage, name='login'),
path('register/', views.registerPage, name='register'),
path('logout/', views.logoutUser, name='logout'),
path('reset_password/', auth_views.PasswordResetView.as_view(),
name="reset_password"),
path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(),
name="password_reset_done"),
path('reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"),
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(),
name="password_reset_complete"),
]
Since you specified an app_name = 'crm', it means the name of the views should be preced with app_name:, so here for example crm:password_reset_confirm.
The urls are written in the views, but we can override these, for example with:
from django.urls import reverse_lazy
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
app_name = 'crm'
urlpatterns = [
path('', views.home, name='dashboard'),
path('login/', views.loginPage, name='login'),
path('register/', views.registerPage, name='register'),
path('logout/', views.logoutUser, name='logout'),
path(
'reset_password/',
auth_views.PasswordResetView.as_view(success_url=reverse_lazy('crm:password_reset_done')),
name='reset_password'
),
path(
'reset_password_sent/',
auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'
),
path(
'reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(success_url=reverse_lazy('crm:password_reset_complete')),
name='password_reset_confirm'
),
path(
'reset_password_complete/',
auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete'
)
]
Django 3.2.5 and Python 3.8.10 - same error
In my case, I had to define an email_template_name for PasswordResetView, and recreate that email template.
# ...
path(
'reset_password/',
auth_views.PasswordResetView.as_view(
success_url=reverse_lazy('crm:password_reset_done'),
email_template_name='my_email.html'
),
name='reset_password'
),
# ...
Following error occurs on runserver command:
In template C:\Users\V\Downloads\DJANGO\blog_project\mysite\blog\templates\blog\base.html, error at line 26
Line 26 in base.html is:
<li>About</li>
In App's URL file:
url(r'^about', views.AboutView.as_view(), name='about')
path('about',views.AboutView.as_view(), name='about'),
Tried both with and without $ sign.
And in views.py file
class AboutView(TemplateView):
template_name = 'about.html'
Below are app as we as project file urls'
App URL file:
from django.urls import path
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.PostListView.as_view(), name='post_list'),
url(r'^about', views.AboutView.as_view(), name='about'),
#path('about/',views.AboutView.as_view(), name='about'),
url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),
url(r'^post/new/$', views.CreatePostView.as_view(), name='post_new'),
url(r'^post/(?P<pk>\d+)/edit/$', views.PostUpdateView.as_view(), name='post_edit'),
url(r'^post/(?P<pk>\d+)/remove/$', views.PostDeleteView.as_view(), name='post_remove'),
url(r'^drafts/$', views.DraftListView.as_view(), name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/comment/$', views.add_comment_to_post, name='add_comment_to_post'),
url(r'^comment/(?P<pk>\d+)/approve/$', views.comment_approve, name='comment_approve'),
url(r'^comment/(?P<pk>\d+)/delete/$', views.comment_remove, name='comment_remove'),
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
]
project URL (mysite)
from django.contrib import admin
# from django.urls import path, include
from django.conf.urls import url, include
from django.contrib.auth import views
urlpatterns = [
url(r'$', include('blog.urls')),
url(r'^admin/', admin.site.urls),
url(r'^accounts/login/', views.LoginView.as_view(template_name='login.html'), name='login'),
url(r'^accounts/logout/', views.LogoutView.as_view(), name='logout', kwargs={'next_page': '/'}),
]
Please follow the patterns as shown below: For old versions of django use the first.
url(r'^about/$', views.AboutView.as_view(), name='about')
path('about/', views.AboutView.as_view(), name='about'),
I have in my main urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('scoring.urls', namespace='scoring')),
]
and in my app urls:
urlpatterns = [
url(r'scoring/(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
url(r'^scoring/$', views.index, name='index'),
]
and in my template:
<li>Scoring</li>
but what {% url 'scoring:index' %}generates is localhost/instead of localhost/scoring. Why?
At first, you can add the ^scoring prefix in the main urls.py file, instead of writing it everywhere in your scoring urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^scoring/', include('scoring.urls', namespace='scoring')),
]
Then in your scoring urls.py make sure to add app_name:
app_name = 'scoring'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
]
(Note that I removed the scoring prefix in the url patterns.)
Now, as you've added app_name, the reversing in your template should work as expected.