Django auth inbuilt view cannot recognize my customized html file - django

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).

Related

django ckeditor image upload

I'm using Django-ckeditor in my website.
I'm especially using
RichTextUploadingField()
in my model. and other option just works fine, except image upload.
1. Error Message
I'm getting an error message of
"Incorrect Server Response" and especially, chrome devtools indicates that
ckeditor.js:21 [CKEDITOR] Error code: filetools-response-error.
ckeditor.js:21 [CKEDITOR] For more information about this error go to https://docs.ckeditor.com/ckeditor4/docs/#!/guide/dev_errors-section-filetools-response-error
2. Guess
I have tried uploading images using ckeditor in my admin page,
authorized as superuser in django, it works.
However, logged in as the normal user account, I've tried the same thing, but it does not work.
So my guess is it has some kind of authorization problem. But I can't figure out where to start debugging in my django-ckeditor.
What things should I be checking? Thanks in advance.
This is happening because the default urls are decorated with #staff_member_required(https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor_uploader/urls.py). To avoid this, instead of including the urls like so url(r'^ckeditor/', include('ckeditor_uploader.urls')) you could define them one by one in your urls.py with the login_required decorator:
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from ckeditor_uploader import views
urlpatterns = [
.....your other urls
url(r'^ckeditor/upload/', login_required(views.upload), name='ckeditor_upload'),
url(r'^ckeditor/browse/', never_cache(login_required(views.browse)), name='ckeditor_browse'),
]
Like this you are limiting the uploads to all users that are logged in.
Add following imports in the project urls.py:
from django.contrib.auth.decorators import login_required
from django.views.decorators.cache import never_cache
from ckeditor_uploader import views as ckeditor_views
Replace the following row in the urls.py:
path('ckeditor/', include('ckeditor_uploader.urls')),
with
path('ckeditor/upload/', login_required(ckeditor_views.upload), name='ckeditor_upload'),
path('ckeditor/browse/', never_cache(login_required(ckeditor_views.browse)), name='ckeditor_browse'),
it works if you are logged in as an admin(localhost:8000/admin), simple is that.

django-allauth caching login and signup pages

is there a way to set up Django Redis caching for login and signup views from django-allauth? I looked at docu and found nothing. I don't want whole site caching but only some views and these two are part of it.
Django Redis makes use of Django's caching framework. So the documentation bit you are looking for is here.
The short bit:
A more granular way to use the caching framework is by caching the output of individual views. django.views.decorators.cache defines a cache_page decorator that will automatically cache the view’s response for you.
For allauth, you'd need to match the login and signup URL before you include allauth.urls and then use the decorator in the url conf:
from django.views.decorators.cache import cache_page
from allauth.account.views import login
urlpatterns = [
url(r'^accounts/login$', cache_page(60 * 15)(login)),
# same for signup
url(r'^accounts/$', include('allauth.urls')
]

Change header 'Django administration' text on nginx

I followed this question's answers to change my django admin panel title header.
I tried this:
There is an easy way to set admin site header - assign it to current
admin instance in urls.py like this
admin.site.site_header = 'My admin'
But it just works when I'm running the page via Python manage.py runserver
My question is how can I change the admin title header when I'm running the site via gunicorn and nginx
writing this code at the bottom of urls.py somehow worked:
admin.site.site_header = 'My admin'
If you already have an admin.py file started wherein you have registered your particular models, you can simply adjust these values there.
your_app/admin.py
# Simple admin setup
from django.contrib import admin
from .models import MyModel
# Register model
admin.site.register(MyModel)
# Tweak admin site settings like title, header, 'View Site' URL, etc
admin.site.site_title = 'My App Admin'
admin.site.site_header = 'My App Admin'
You can find all the attributes here.
follow the below steps to customize the site header and site title text of django admin login page :
1.)First import admin module in settings.py file like as below :
from django.contrib import admin
2.)In bottom of settings.py file add these lines:
admin.site.site_header = 'MY_SITE_HEADER'
admin.site.site_title = 'MY_SITE_TITLE'
The above method works in latest version of django i.e.1.11.3 till date.
You can make changes to parts of the admin by providing a template in an admin subdir of your templates directory to override what is provided by admin.
In this case, you'd want to provide a base_site.html template. You can see what the default one looks like here: https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base_site.html

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.

Is there a way to render a html page without view model?

Is there a way to render a html page without having a view model in django if a page is going to display only static html?
Also, can I redirect to a html page instead of a url? For example, instead of doing this:
return HttpResponseRedirect('form/success/')
can I do this:
return HttpResponseRedirect('success.html')
?
direct_to_template no longer works in Django 1.8. Here is how to do it in 1.8, in the urls.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="your_static.html"), name='whatever'),
]
You can render a template without a view using the generic view direct_to_template.
Redirecting to success.html isn't the way to go in django since success.html doesn't have an associated URL. You always have to bind the template to an url via a view (direct_to_template is a view which gets all its arguments from the conf in urls.py, but it's still a view)
For static HTML you could use Flatpages app.
You could also serve rendered templates (which could contain only static HTML) but you will need a view:
from django.shortcuts import render_to_response
def some_view(request):
return render_to_response('sometemplate.html')
About redirection, basically you can't redirect to HTML page by just giving the filename, even if it were statically served by the web server you would still be using a URL that points to that file.
I'm pretty sure this isn't what OP wanted to do, but if someone wants to render something without a view, (for sending an email or appending to other html code), you can use this:
from django.template.loader import render_to_string
html = render_to_string('template.html', params)
Use lambda function.
urlpatterns = [
path('', lambda request: render(request, 'homepage.html'))
]