urls.py
from django.conf.urls import patterns, include, url from django.conf.urls.i18n import i18n_patterns from django.utils.translation import ugettext_lazy as _ from django.contrib import admin admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^i18n/', include('django.conf.urls.i18n')), )
urlpatterns += i18n_patterns('',
(_(r'^dual-lang/'), include('duallang.urls')),
(r'^', include('home.urls')), )
from django.conf import settings
if 'rosetta' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
url(r'^rosetta/', include('rosetta.urls')),
)
Getting ImportError: cannot import name 'patterns' from
'django.conf.urls'
you must convert pattern to list, url can be conveted to path or re_path like this:
from django.urls import path, include
urlpatterns = [
path('admin/', include(admin.site.urls)),
path('i18n/', include('django.conf.urls.i18n'))
]
i18n_pattern can be used like this:
from django.conf.urls.i18n import i18n_patterns
from django.urls import path, include
urlpatterns += i18n_patterns(
path('', include('home.urls'))
)
Related
I have a HomeView Class which I added a login_required, but I don't know how to redirect it with the custom login page that I made.
Here are the urls.py and settings.py in my project:
myblog\urls.py
from django.contrib import admin
from django.urls import path,include
from .settings import DEBUG, STATIC_URL, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT
from django.conf.urls.static import static
# from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myblog_app.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
# + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
if DEBUG:
urlpatterns += static(STATIC_URL,document_root =STATIC_ROOT )
urlpatterns += static(MEDIA_URL,document_root = MEDIA_ROOT)
myblog_app\urls.py
from django.urls import path
from django.contrib.auth.decorators import login_required
from .views import HomeView,ArticleDetailView,AddPostView,UpdatePostView,DeletePostView,AddCategoryView,CategoryView,CategoryListView,LikeView,AddCommentView
urlpatterns = [
path('', login_required(HomeView.as_view()),name='home'),
path('article/<int:pk>', ArticleDetailView.as_view(),name='article-detail'),
path('add_post/', AddPostView.as_view(),name='add_post'),
path('article/<int:pk>/comment/', AddCommentView.as_view(),name='add_comment'),
path('article/edit/<int:pk>', UpdatePostView.as_view(),name='update_post'),
path('article/<int:pk>/remove', DeletePostView.as_view(),name='delete_post'),
path('add_category/', AddCategoryView.as_view(),name='add_category'),
path('category/<str:cats>', CategoryView,name='category'),
path('category-list', CategoryListView,name='category_list'),
path('like/<int:pk>', LikeView,name='like_post'),
]
members/urls.py
from django.urls import path
from .views import UserRegisterView,UserEditView,UserLoginView,PasswordsChangeView,ShowProfilePageView,EditProfilePageView
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('login/',UserLoginView, name='login'),
path('register/',UserRegisterView.as_view(), name='register'),
path('edit_profile/',UserEditView.as_view(), name='edit_profile'),
# path('password/',auth_views.PasswordChangeView.as_view(template_name='registration/change-password.html')),
path('password/',PasswordsChangeView.as_view(template_name='registration/change-password.html')),
path('password_success/',views.password_success,name='password_success'),
path('<int:pk>/profile/',ShowProfilePageView.as_view(),name='show_profile_page'),
path('<int:pk>/edit_profile_page/',EditProfilePageView.as_view(),name='edit_profile_page'),
]
settings.py
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL='home'
LOGOUT_REDIRECT_URL='login'
What it's doing now is that it basically just redirects to the
path('', login_required(HomeView.as_view()),name='home') and it doesn't know where to get the login url from, so instead it should redirect to path('login/',UserLoginView, name='login') but I have no idea how to do it
Okay I found the answer, so I just add in login_url='([login url path])' so that it redirects to the login url that I wanted... now it should look like this:
path('',login_required(HomeView.as_view(),login_url='/members/login/'),name='home'),
*from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('products/',include('products.urls')),
path('accounts/',include('accounts.urls')),
]*
The above code of pyshop/url.py
I could not login to admin page.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('products/new',views.new),
path('products/',views.products),
path('accounts/register',views.register),
]
The code is of product/urls.py.
Old question, but I run into the same problem, so just in case I can help someone else.
I would put in the pyshop/url.py, this code (see the admin path is the only one with an actual url):
from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('products.urls')),
path('',include('accounts.urls')),
]
Then in your product/urls.py (See all of the routes end with a slash):
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="product"),
path('products/new/',views.new, name="new_product"),
path('products/',views.products, name="products"),
path('accounts/register/',views.register, name="register"),
]
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
So I have code below that is in django 1.8
from django.conf.urls import patterns, url
from account import views
from django.contrib.auth import views as auth_views
urlpatterns = patterns('',
url(r'^$', views.index, name='profile'),
url(r'^api/get_users/(?P<term>.*)', views.get_users),
url(r'^leaderboard/(?P<board_type>.*)', views.leaderboard),
url(r'^admintools/(?P<action>.*)', views.admintools),
)
I modified it to django 2.2
from django.conf.urls import url
from . import views
from django.urls import re_path,path
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.index, name='profile'),
path('api/get_users/(?P<term>.*)', views.get_users),
path('leaderboard/(?P<board_type>.*)', views.leaderboard),
path('admintools/(?P<action>.*)', views.admintools),
]
I get the error The current path account/admintools, didn't match any of these
"one of the easy solutions" to this problem is, use re_path(...) instead of path()
from django.urls import re_path
from account import views
urlpatterns = [
re_path(r'^$', views.index, name='profile'),
re_path(r'^api/get_users/(?P<term>.*)', views.get_users),
re_path(r'^leaderboard/(?P<board_type>.*)', views.leaderboard),
re_path(r'^admintools/(?P<action>.*)', views.admintools),
]
The re_path(...) function will do the same thing as the Django url(...) did.
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'),
]