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

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.

Related

Django views and urls - can't runserver

I'm learning django and having issues with implementing this code below:
from django.urls import path
from . import views
urlpatterns = [
path('members/', views.members, name='members'),
]
The code above is stored in a urls.py file I created in an app folder as directed by the tutorial guide. However when I try to run it on the browser I get this message:
The included URLconf '<module 'members.urls' from 'C:\Users\Chinasaokwu\Desktop\storefront\try-django\my_app\members\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
I don't know what to do here. I'm new to django.
It's w3schools.com that I am using as a guide
I'm still learning how views and urls work since I'm new to django but I understand that views are functions that take and return https requests/response, and that urls are used to implement views.

Applying ratelimit decorator to auth views.py

I want to rate limit several views in my Django app (login, register_account, password reset, ...). I am already using Django-Ratelimit. But I am unsure how to add this decorator to existing views. Writing my own views and using them in a custom urls.py looks like a lot of boilerplate code just to add some decorators.
You can use decorators directly in your urls.
url(r'^login/$', ratelimit(key='whatever')(login_func), name='login'),

Django auth inbuilt view cannot recognize my customized html file

I am trying to use django's inbuilt urls and views for the user authentication but have customized the html files, e.g. login.html registration/password_reset_form.html
I have imported the urls in my url.py
from django.contrib.auth import urls
and in the urlpatterns
url(r'^account/', include('django.contrib.auth.urls')),
in my views.py
from django.contrib.auth.views import *
(with no other view functions to handle the auth process)
in my registration file there are login.html password_reset_form.html password_reset_done.html...
The problem is that the django view login is recognizing my login.html under the registration file as it shows my customized log in page, but for the url account/password_reset/ the django password_reset view function cannot recongnize my password_reset_form.html, instead, it is using the django password_reset page.
Can anyone tell me where the problem could be and how to solve this problem?
I have read the django auth codes here https://github.com/django/django/tree/master/django/contrib/auth
and really want to use the django inbuilt url/views to make my project standard. Thank you very much.
The filesystem template loader uses settings.TEMPLATE_DIRS to specify where to look for templates, so make sure that you've added the directory containing registration/ to it (and that you've enabled the filesystem loader).

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

Django static page?

I want to make a static page which will be shown to the user only if he/she clicks on a link provided in one of my models. I can do this by making a Python page alone and calling it, but I want it be called from Django. The user interface should be constructed using the Django API only.
Any suggestions?
With the class-based views in newer Django versions, one can use this in urls.py:
from django.views.generic import TemplateView
url(r'^about',
TemplateView.as_view(template_name='path/to/about_us.html'),
name='about'),
Bypassing views to render a static template, add this line in "urls.py". For example "About Us" page could be
(r'^about', 'django.views.generic.simple.direct_to_template', {'template': 'path/to/about_us.html'}),
Do you mean something like Django's flatpages app? It does exactly what you describe.
If you want to make a static page the flatpages is a good choice. It allows you to easily create static content. Creating static content is not harder than creating a view really.
On Django 2.2.6, loosely following David's answer, I added the path in urls.py:
from django.views.generic import TemplateView
urlpatterns = [
.... .... ....
path('about',
TemplateView.as_view(template_name='path/to/about_us.html'),
name='about'),
And I needed to adjust settings.py to specify the template directory:
TEMPLATES = [{
... ... ...
'DIRS': [os.path.join(BASE_DIR, 'template')],
Then I saved the actual content in template/path/to/about_us.html