how to get previous page url in django template? - django

I don't have the views.py using PasswordResetViews!
i am using the django password reset but i want my template to behave differently when it come from password_rest_email and from password_reset_confirm
urls.py
from django.conf.urls import url,include
#from django.urls import path
from dappx import views
from django.contrib.auth.views import PasswordResetView,PasswordResetDoneView,PasswordResetCompleteView,PasswordResetConfirmView
# SET THE NAMESPACE!
app_name = 'dappx'
# Be careful setting the name to just /login use userlogin instead!
urlpatterns=[
url('register/', views.register, name='register'),
url('user_login/', views.user_login, name='user_login'),
url('google_login/', views.google_login, name='google_login'),
url('special/', views.special, name='special'),
url('logout/', views.user_logout, name='logout'),
url(r'^', include('django.contrib.auth.urls')),
url('password_reset/', PasswordResetView.as_view(), name='password_reset'),
url('password_reset/done/', PasswordResetDoneView.as_view(), name='password_reset_done'),
url('reset/<uidb64>/<token>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
]
i need that template i.e(password_reset_confirm) to show email is send when it come from password_reset_email and show password reset successful if it come from password_reset_confirm
password_reset_complete.html
{% extends 'dappx/base.html' %}
{% block title %}Check your Email{% endblock %}
{% block body_block %}
<div class="row no-gutters">
<div class="col-12 col-sm-6 col-md-8"></div>
<div class="col-6 col-md-4">
<div class="jumbotron">
<h4>The password reset link has been sent to youre Email</h4>
<br>
<p>Check your email to reset the password. You can log in now on the log in page.</p>
<br>
</div>
</div>
{% endblock %}

Seems duplicate of Django: getting previous url
You can always pass GET variable with information you want and in subsequent view, it can be used directly.

I can not comment posts yet, so I just write this as an answer. The following link provides a tutorial about the django built in authentication. There is logic for a view that tells the user that an email has been sent 'Password Reset Done View' and logic for a view that tells if it was successfull 'Password Reset Complete View'.
Django Tutorial

Related

Django: custom button in admin change form return bad url for custom view function

I have problems to link my custom button from change_form.html to my view function.
pic : admin change form custom button
change_form.html
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block title %} Send Email {% endblock %}
{% block content %}
{% if request.resolver_match.url_name == 'requests_requests_change' %}
<div class="submit-row">
{% csrf_token %}
<a href="{% url 'requests:send-email' original.pk %}"
class="button" style="background-color: #F08000;float: left">Request Subscription
</a>
</div>
{% endif %}
{{ block.super }}
{% endblock %}
views.py
from django.shortcuts import render, redirect
def send_email(request, requests_id):
return redirect('') # or whatever to test the url
urls.py
from django.urls import path
from . import views
app_name = 'requests'
urlpatterns = [
path('<int:pk>/send-email/', views.send_email, name='send-email'),
]
main 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.site.urls), # admin site administration
path('requests/', include('requests.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
pic: error message
Any help would be great! Thanks!
I have found the solution on my own.
The issue was the url inside the template.
Worked with this one:
Template :
<a href="{% url 'admin:requests_requests_change' original.pk %}send-email/"
And the URL path:
path('<int:pk>/change/send-email/', views.send_email, name='send-email')

Login page in python django

Hi i'm just trying to create my login page but i have an error. i want to import the Login View and use the username and password from the admin that i created.
i tried this code but i have an error.
urls.py
from django.contrib import admin
from django.urls import path
from gomo_websys.views import login_page, home_page
from django.contrib.auth.views import LoginView
urlpatterns = [
path('' , LoginView, {'template_name': 'login/Login.html'}),
path('home/' , home_page, name='home_page'),
Login.html
{% extends 'base.html' %}
{% block head %}
<title>Login</title>
{% endblock %}
{% block body %}
<h1>Welcome</h1>
<p>
You can log in here!
</p>
<h2>Login</h2>
<form method="post">
{% crsf_token %}
{{ form.as_p }}
</form>
{% endblock %}
views.py
from django.http import HttpResponse
from django.shortcuts import render
def login_page(request):
return render(request, "login/Login.html")
i have an error result.
In urls.py
from django.urls import path, include
from django.contrib.auth import views
urlpatterns = [
path('',include('django.contrib.auth.urls')),
]
so now you can got to example.com/login to go to default login page by django
so now you can got to example.com/logout to logout
To use custom login page , make a folder called registraions in templates folder and your login html page with login.html name
you can control on which you land after login and logout from below code
LOGIN_URL = 'login'
#'login' is the url where you go if you use #login_required decorator if you are not logged in
LOGIN_REDIRECT_URL = 'index'
# 'index' is url where you go after logging in
LOGOUT_REDIRECT_URL = 'login'
# 'login' is url where you go after logged out

error while using "django.contrib.auth.views.login"

i am getting error "view must be a callable or a list/tuple in the case of include()." while trying to use django's built-in Login system (login,logout,logout_then_login). can anyone please sort this out.
bookmarks/accounts/urls.py-
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login',
name='logout_then_login'),
]
bookmarks/urls.py-
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/',include("account.urls"))
]
templates/registration/login.html-
<body>
<h1>Log-in</h1>
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% else %}
<p>Please, use the following form to log-in.
{% endif %}
<div class="login-form">
<form action="{% url "login" %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Log-in"></p>
</form>
</body>
templates/registration/logged_out.html-
<h1>Logged out</h1>
<p>You have been successfully logged out. You can <a href="{% url "login"
%}">log-in again</a>.</p>
</body>
I am assuming you are using django>1.9: So you cannot use strings as views anymore. So you need to do something like this with all views:
from django.contrib.auth.views import login
#.....
url(r'^login/$', login, name='login'),
...
And also with the include:
from django.contrib import admin
from accounts import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/',include(urls))
]

Password reset in Django-registration can't find site.domain

My password_reset_email.html in django-registration looks like this:
{% blocktrans %}
To reset your password, please click the following link:
{% endblocktrans %}
<body>
<p>
<a href="http://{{ site.domain }}{% url 'auth_password_reset_confirm' uid token %}">
Reset password
</a>
</p>
</body>
{% blocktrans %}
Django is picking up the url but not {{site.domain}}. Yet, when I have the same code in the registration process {{site.domain}} was valid. What makes the password_reset_email.html different than the registration process?
Thanks.
django-registration (or rather django.contrib.auth) doesn't use context processors for rendering emails. You will have to add {{ site }} to the context manually during rendering. Basically you'll have to customize django-registration's urls. Something along those line:
from django.contrib.sites.models import Site
from django.contrib.auth import views as auth_views
....
url(r'^password/reset/$', auth_views.password_reset,
{'post_reset_redirect': reverse_lazy('auth_password_reset_done'),
'extra_email_context': {'site': Site.objects.get_current()}},
name='auth_password_reset'),
....

Django: Next value is changing oddly in login page

I wanted to redirect the users in my app to their users page if they were already logged in and tried to go directly to "../login/". I've found this answer:
Django: Redirect logged in users from login page
It works fantastic until I decide to hit the "Registration" link I have below my login fields. I don't know why but when I hit it, I get redirect to the login page again but the only thing that changes is the url, for some reason it becomes "http://localhost:8000/users/login/?next=/users/register/", and it wont take me to my registration page.
Why the "next" variable changes if I've set it with another url in the login template like so:
{% extends "base.html" %}
{% block title %}User Login{% endblock %}
{% block head %}User Login{% endblock %}
{% block content %}
{% if form.errors %}
<p>User name or password is incorrect.</p>
{% endif %}
<form method="post" action="{% url login %}">
{% csrf_token %}
<p><label for="id_username">Username:</label>
{{ form.username }}</p>
<p><label for="id_password">Password:</label>
{{ form.password }}</p>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="users/"/>
</form>
<li>Register</li>
{% endblock %}
I'm using django1.4 and python 2.7. My urls.py are:
For the whole application:
urlpatterns = patterns('',
url(r'^$', main_page, name="main_page"),
url(r'^users/',include('user_manager.urls')),
)
For the user_manager module:
urlpatterns = patterns('user_manager.views',
url(r'^$', users, name="user_page"),
url(r'^logout/$', user_logout, name="logout"),
url(r'^login/$', user_login, name="login"),
url(r'^(\w+)/$', user_page),
url(r'^register/$', register_page),
)
Do you have a #login_required decorator on that register_page view? If so, remove it
Ok I've found the problem. Django checks the urls regex in order so the "register" url was matching the "(\w+)/" intended to work for the users pages. So all I had to do is put that url at the end, the urls.py now looks like this:
urlpatterns = patterns('user_manager.views',
url(r'^$', users, name="user_page"),
url(r'^logout/$', user_logout, name="logout"),
url(r'^login/$', user_login, name="login"),
url(r'^register/$', register_page),
url(r'^(\w+)/$', user_page),
)
Nevertheless I still don't understand quite well why that mistaken match was changing the next value. I know that caused it but I don't know why...
Thank you very much for your time!