combining django-authtools and django-allauth in a Django project - django

I am having a Django (1.8) project with allauth integrated. Now I would like to have a custom user model instead of the auth.user one (which I am aware is configurable, but I need more flexibility). When I follow the django-authtools installation instruction one step is to add following line to the url-patterns, see
https://django-authtools.readthedocs.org/en/latest/intro.html#installation
url(r'^accounts/', include('authtools.urls')),
However this URL pattern is already taken by the allauth app (in line the instructions thereof, see
http://django-allauth.readthedocs.org/en/latest/installation.html
):
url(r'^accounts/', include('allauth.urls')),
So how should I proceed?

It doesn't really matter on what URL will you include that additional patterns, you can change URL both for allauth and authtools, because that URL is not hardcoded anywhere, it will be reversed automatically by those apps when needed. So you can for example use:
url(r'^auth/', include('authtools.urls')),
url(r'^accounts/', include('allauth.urls')),
You can also include both pattern groups on one url prefix, but if there is conflict (for example both allauth and authtools registers `accounts/login/' URL), django will serve view for first entry in urlconf.

Related

How do I reference a path (URL) in Django? But this path is not in the base app, but in another app

In the base app, which I call it "mywebsite" (the one that contains the settings of the django project), has the urls.py file. But I do not want to reference this file, I want to reference a urls.py in another app, which I call it "account".
For the base file, I would reference as {% url'login' %}, for example. How do I reference to the other one?
Maybe {% account.url 'login' %}?
I tried {% account.url 'login' %} and {% account/url 'login' %}
Never mind, I just added to the base urls.py file: path('account', include("account.urls"))
I see that you already answered your question, but with no further details or explanations. I'll provide the context needed.
You have a project package. This folders contains files that affect all the application (the website, not just an app). Inside we have the following files: settings, wsgi, asgi and urls.
In the urls.py (also called URLconf module) file you control the routing of your application, that is, the mapping between URL path expressions to Python functions (your views).
The Django frameworks knows where to look up for this file because it's stablished in the settings.py file:
ROOT_URLCONF = 'django_project.urls'
This is the process (from the docs)
Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting...
Django loads that Python module and looks for the variable urlpatterns. This should be a sequence of django.urls.path() and/or django.urls.re_path() instances.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view)...
But you also have apps, besides the project package. Each app can also have a URLconf module (app/urls.py).
To Django to know the routing to this apps, you have to include them in the global urls.py file (from the project package).
Finally, the answer:
How do you do it? With the include() function.
As you mentioned in your own answer, the code is:
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("account.urls")),
]
You're including the accounts paths into the global URLconf module.
So when the user types www.your-domain.com/ he will access the accounts app routes.
If instead you did like below, the user should type: www.your-domain.com/accounts/.
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include("account.urls")),
]

Is it possible to implement django login-authentication with out writing my own template?

my project's urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('^', include('django.contrib.auth.urls')),
]
and, my app url's are:
from django.conf.urls import include, url
urlpatterns = [
url(r'^login/$', django.contrib.auth.views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', django.contrib.auth.views.logout, {'template_name': 'logout.html'}, name='logout'),
]
I'm learning about django authentication. As after reading some docs, I've made a sample app and tried to implement the django authentication system on it.
Is there any built-in django template for these built in views. I've tried it without writing my own template but it throws exception. When I add my login template it works fine.
Is it possible to implement django-authentication with out writing my own 'login.html' template? Is django really having any built-in one? If it is, then how can I include it in my app?
Thanks! in advance
No, Django does not come with a login template. From the authentication docs:
It’s your responsibility to provide the html for the login template, called registration/login.html by default.
If Django came with a login template, it would probably look basic. Django doesn't know template structure, so wouldn't be able to inherit from your base template. So most people would end up creating a custom login template anyway. However, it would be easier for new users if Django included a template, however basic it looked.
Note that the authentication docs includes a sample template that you can use.
As an aside, you should either include 'django.contrib.auth.urls', or add entries for login and logout to your app's urls (this allows you to override the template name). You don't need to do both.

Zinnia Blog Entry showing up 404 Page Not Found

I have Django 1.5.1 installed and django-cms 2.4.2
However I have not integrated zinnia blog and django-cms just yet.
I was able to create a blog entry but when going to the blog entry
8000/en/weblog/2013/10/13/test-entry/
I receive a 404 Page not Found
any thoughts?
Possible cause: order of urlpatterns inclusions in urls.py. Django-Cms design lack.
Fix: put cms.urls after zinnia.urls:
# patterns or i18n_patterns here.
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('zinnia.urls')),
url(r'^comments/', include('django.contrib.comments.urls')),
url(r'^', include('cms.urls')),
)
Explanation:
If you include cms urls before zinnia urls, django-cms "slug" pattern matches a wide range of URLs including Zinnia blog entry URL:
<RegexURLPattern pages-details-by-slug ^(?P<slug>[0-9A-Za-z-_.//]+)/$>
As an example, it will match: "blog/2014/01/20/test-article-about-something/"
After it matched as django-cms:pages-details-by-slug, the whole URI is stored in the "slug" variable and is provided as an argument (in "kwargs") to cms.views.detail view function. And this view will call:
cms.utils.page_resolver import get_page_from_request(request, use_path=slug)
and cms will not find (and will raise "Resolver404" exception) any suitable page to render because this URI belongs to Zinnia blog.
End of story.
Resources:
"details" view can be found here: cms.views
django url resolving logic is here: django.core.urlresolvers mainly in two "resolve" methods. (lines: 315 *recursive, 209 *non-recursive)

Django: Views problem with Django + django-registration + jinja

So, I have a django project that is using jinja2 rendering, and I also installed django-registration to make my life easier. I ran into the following problem:
Going to homepage I render it with jinja. In order to check for authentication, I have to use jinja's syntax, which is user.is_authenticated(). However, in regular django templating, this check is done with user.is_authenticated. If in regular django templating there are (), it gives error.
So going to the /accounts/login/ page, the django-registration modul doesn't do anything special, so it forwards the url to the standard django views the following way:
from django.contrib.auth import views as auth_views
url(r'^login/$',
auth_views.login,
{'template_name': 'registration/login.html'},
name='auth_login'),
So I know for sure I shouldn't be changing the django.contrib.auth view, but then where do i put my own view? In myapp/views.py?
And also, do I have to copy paste the django view, and then modify on top of it (in this case simply replace the render with render_jinja) or is there a way to 'extend' this original django view to my own slightly modified view for logging in?
Whether right or wrong, in the registration module, I made a new view, that handled the logging, copying a few lines from here and there. It logical and seems to be working fine.

Adding forgot-password feature to Django admin site

How to add the forgot-password feature to Django admin site? With email/security question options? Is there any plug-in/extension available?
They are all there built in the django. Just add the relevant url patterns. As follows.
from django.contrib.auth import views as auth_views
patterns+=('',
url(r'^passreset/$',auth_views.password_reset,name='forgot_password1'),
url(r'^passresetdone/$',auth_views.password_reset_done,name='forgot_password2'),
url(r'^passresetconfirm/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$',auth_views.password_reset_confirm,name='forgot_password3'),
url(r'^passresetcomplete/$',auth_views.password_reset_complete,name='forgot_password4'),
)
And, oh, while you are at it, also add the views and url patterns for password change.
url(r'^password/change/$',
auth_views.password_change,
name='auth_password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='auth_password_change_done'),
They are listed in the documentation of course.
You'll have to provide your own templates.
Actually since Django 1.4 there's an easy way to get the forgotten password link appear directly in the admin login page (which sounds like the precise question asked):
https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#adding-a-password-reset-feature
You can add a password-reset feature to the admin site by adding a few
lines to your URLconf. Specifically, add these four patterns:
url(r'^admin/password_reset/$',
'django.contrib.auth.views.password_reset',
name='admin_password_reset'), (r'^admin/password_reset/done/$',
'django.contrib.auth.views.password_reset_done'),
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$',
'django.contrib.auth.views.password_reset_complete'),
(This assumes
you’ve added the admin at admin/ and requires that you put the URLs
starting with ^admin/ before the line that includes the admin app
itself).
Changed in Django 1.4 The presence of the admin_password_reset named
URL will cause a “forgotten your password?” link to appear on the
default admin log-in page under the password box