Django==1.11.2
django-registration-redux==1.6
When I'm trying to reset password (http://localhost:8000/accounts/password/reset/), I occur at a page with headline Django administration, and breadcrumbs below: Home › Password reset. So, this is a part of Django functionality. This may be important, though, I don't know how. But anyway, this is not the functionality of django-registration-redux.
I input an email. And get this:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
In django-registration-redux another name is used. Namely auth_password_reset_confirm.
Well, could you give me a kick here? My settings are below:
settings.py
INCLUDE_REGISTER_URL = True
INCLUDE_AUTH_URLS = True
urls.py
urlpatterns = [
url(r'^accounts/', include('registration.backends.default.urls')),
}
As mentioned in the bug report, registration must be earlier in order then admin. Not entirely sure why and apparently it's hard to fix, as this package is maintained by several Django core team members :).
From the docs, https://django-registration.readthedocs.io/en/2.2/quickstart.html#quickstart: django.contrib.auth must be in INSTALLED_APPS, I suggest adding before registration so that the package can override the core
Related
Django allauth fails when using a custom user model with the email as the primary key, I learnt about this when I tried to test the password change functionality.
Reverse for 'account_reset_password_from_key' with keyword arguments '{'uidb36': 'mgodhrawala402#gmail.com', 'key': 'bbz25w-9c6941d5cb69a49883f15bc8e076f504'}' not found. 1 pattern(s) tried: ['accounts/password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$']
I don't mind going into the code to fix this issue, since my application is still under development, I can still change authentication incase anyone has any suggestions.
The easiest fix for this problem would be adding a sequence integer pk and just updating your email field to index unique because as you see the regex path doesn't accept email format for uidb36, it only accepts 0-9A-Za-z.
If you must use email as pk you can add another path to your urls to accept your email pattern:
from allauth.account import views
path('accounts/', include('allauth.urls')),
re_path(
r"^accounts/password/reset/key/(?P<uidb36>.+)-(?P<key>.+)/$",
views.password_reset_from_key,
name="account_reset_password_from_key_accept_email",
),
Ive got a users profile page and an update_profile page.
My projects urls.py:
re_path(r'^profile/(?P<username>[\w-]+)/$', include('users.urls')),
My users.urls:
path('', views.profile, name='profile'),
path('update_profile', views.update_profile, name='update_profile'),
Prior to adding the username argument to the url both of these links were working. Since adding the username argument, I can access the profile page but the update_profile page throws a 404 error. My understanding is that if the address bar reads www.site/profile/testuser/update_profile the projects urls.py will strip the www.site/profile/testuser/ and pass just update_profile to the profile apps urls.py, which then should match the path ive given.
Why isnt this working?
On my profile page I have a check that ensures the username passed in matches the logged in user (so users can only access their own profiles). I will need a similar check on the update_profile page, but currently arent passing username to the update profile page. How could I perform this check without passing it in a second time like /profile/testuser/update_profile/testuser?
Thank you.
These are the answers to your questions:
1. Your url is not working because of there is a $ at the end of users.urls url. $ is a zero width token which means an end in regex. So remove it.
2. You do not need to add <username> at the profile_update url. If you are using UpdateView, then add slug_url_kwarg attribute to UpdateView, like:
class MyUpdateView(UpdateView):
slug_url_kwarg = "username"
If you are using function based view, then simply use:
def myview(request,username):
....
Django 3.0.6
urlpatterns = [
path('', HomeView.as_view(), name='home'),
path('{}'.format("admin/" if DEBUG else "dhjfsljdasdhje32/"), admin.site.urls), # Change admin url for security reasons.
path('image/', include(('image.urls', 'image'), namespace="image")),
path('polls/', include(('polls.urls', 'polls'), namespace="polls")),
path('applications/', include(('applications.urls', 'applications'), namespace="applications")),
]
def _get_categories_url_pattern():
"""
Organize urls with posts categories.
URL format:
<category>/<post_slug>
Example: linux/how_to_install_ubuntu/
"""
categories = Category.objects.all().values_list("slug", flat=True)
for category in categories:
urlpatterns.append(path('{}/'.format(category), include('post.urls')))
urlpatterns.append(path('draft/{}/'.format(category), include('post.urls')))
_get_categories_url_pattern()
Please, concentrate your attention on how categories in urls are handled.
Problems with this code:
When a new category is added in the admin site, Django project has
to be relaunched (at least with the built in dev server).
When last time I did python manage.py makemigrations it blew up. I
had to comment out anything having anything to do with
_get_categories_url_pattern. Then it made migrations. Now I can't reproduce this error anymore. But there seems to be some danger in this code.
Could you help me understand how to refactor this code for it to work at least without restarting the dev server when a new category is added.
The urls are loaded when the server starts, so you should avoid doing database queries there.
When you run migrate for a fresh database, your code will give the error because the category table hasn't been created yet. Once you have done the first migration, the code will run without error, but as you have noticed, the URLs will not update as new categories are added.
The normal approach is to add a slug kwarg to the URL pattern.
path('applications/', include(('applications.urls', 'applications'), namespace="applications")),
path('<slug:slug>', include('post.urls')),
path('draft/<slug:slug>', include('post.urls')),
Then in the view, use get_object_or_404 to make sure that the category exists.
from django.shortcuts import get_object_or_404
def category_view(request, slug):
category = get_object_or_404(Category, slug=slug)
...
One problem with this, is that <slug:slug>/ will match other URLs, e.g. /image/ or /polls/. In the above code, I've avoided that problem by putting <slug:slug> at the bottom of the URL patterns. Another option would be to use something like categories/<slug:slug>/ so that it doesn't match other URLs.
When I attempt to access my wagtail back-office at /cms/, I get redirected to wagtail's login page, /cms/login/.
However, I would like to use my own custom login, which is default for the rest of the site, and sits at /auth/.
My LOGIN_URL is already set to /auth/ in django settings.
EDIT : it's been suggested that this is a generic question of how do you override namespaced url patterns but this is not the case. The urls are not namespaced, and I was looking for wagtail functionality that adressed this specific issue. Fortunately, that functionality does exist.
WAGTAIL_FRONTEND_LOGIN_URL suggested above is specifically intended just for front end users and there is not an equivalent setting for admin users. You could use redirect_to_login like so:
from django.contrib.auth.views import redirect_to_login
from django.urls import reverse
from wagtail.admin import urls as wagtailadmin_urls
def redirect_to_my_auth(request):
return redirect_to_login(reverse('wagtailadmin_home'), login_url='myauth:login')
urlpatterns = [
url(r'^cms/login', redirect_to_my_auth, name='wagtailadmin_login'),
url(r'^cms/', include(wagtailadmin_urls)),
]
The Wagtail setting WAGTAIL_FRONTEND_LOGIN_URL allows you to configure how users login to the Wagtail admin.
From http://docs.wagtail.io/en/v1.10.1/advanced_topics/privacy.html#setting-up-a-login-page:
If the stock Django login view is not suitable - for example, you wish to use an external authentication system, or you are integrating Wagtail into an existing Django site that already has a working login view - you can specify the URL of the login view via the WAGTAIL_FRONTEND_LOGIN_URL setting
To elaborate on Erick M's answer, since this is the working answer:
You do need to set the correct permission (wagtailadmin.access_admin) or set the is_superuser flag in Django's auth_user database table to be able to access the CMS, otherwise you still get a "permission denied" error.
I thought this had to do with my implementation, but it was already working, but failed because of the above reason.
I'm still a novice and I'm not sure how I should setup the profile page for users. The django.contrib.auth.views.login will redirect to a accounts/profile page, but how should I actually set this up?
Is it common to just define accounts/profile in urls.py and have it redirect to a template or is it a more complex pattern?
django.views.generic.simple.direct_to_template, {'template':'profile.html'}) where profile.html lives in PROJECT_ROOT/templates/profile.html and extends base.html?
You can either set up a view/url that points to accounts/profile, or you can tell Django to redirect somewhere else after login by setting the LOGIN_REDIRECT_URL param in your settings.py.
There are some reusable apps out there for handing common profile page functionality, take a look at https://bitbucket.org/ubernostrum/django-profiles/wiki/Home or http://code.google.com/p/django-profile/