Signup page upon submission leads to error Django - django

I have built a site, where the user upon successful signup should be redirected to the login page. However it is not occurring so. Attached below are the relevant files:
Signup page html:
{% extends "base.html" %}
{% load bootstrap4 %}
{% block content %}
<div class="container">
<h1>Sign Up</h1>
<form method="POST" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Sign Up</button>
{% endbuttons %}
</form>
</div>
{% endblock %}
Urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
path('logout/', auth_views.LogoutView.as_view(), name="logout"),
path('signup/', views.SignUp.as_view(), name="signup"),
]
views.py:
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("login")
template_name = "accounts/signup.html"
Any help is appreciated. Thank you.

Related

Page not found (404) Request Method:

please help me. I don't know why can't see the detail of product when i click a product. both first list work good just when i need to see detail of product in second list(filtered list) an 404 error shows.
thanks
here is my code below:
my urls:
from django.urls import path
from . import views
app_name = 'shop'
urlpatterns = [
path('', views.HomeView.as_view(), name='home'),
path('categories/<category>/', views.ProductListView.as_view(), name='product-list'),
path('categories/<int:pk>/', views.ProductDetailView.as_view(), name='product-detail'),
]
views:
from django.shortcuts import render, get_object_or_404, get_list_or_404
from django.views import generic
from .models import Category, Product
class HomeView(generic.ListView):
model = Category
template_name = 'home.html'
class ProductListView(generic.ListView):
template_name = 'product_list.html'
def get_queryset(self):
self.category = get_object_or_404(Category, title=self.kwargs['category'])
return Product.objects.filter(category=self.category)
class ProductDetailView(generic.DetailView):
model = Product
template_name = 'product_detail.html'
product_list.html
{% extends 'base.html' %}
{% load static %}
{% block title %}
Welcome | Global Market
{% endblock %}
{% block content %}
{% for product in product_list %}
<a href="{% url 'shop:product-detail' product.pk %}">
<div class="card bg-light mb-3" style="max-width: 18rem;">
<div class="card-header">{{ product.title }}</div>
<img class="card-img-top" src="{{ product.image.url }}" alt="{{ product.title }}">
<div class="card-body">
<p class="card-text">{{ product.description }}</p>
</div>
</div>
</a>
{% endfor %}
{% endblock %}
product_detail.html
{% extends 'base.html' %}
{% load static %}
{% block title %}
Welcome | Global Market
{% endblock %}
{% block content %}
<h1>thid</h1>
{{ product.name }}
{% endblock %}
Even if the you visit /categories/1, it will use the ProductListView, since the str path converter also accepts a sequence of digits. You can swap the paths, so:
from django.urls import path
from . import views
app_name = 'shop'
urlpatterns = [
path('', views.HomeView.as_view(), name='home'),
path('categories/<int:pk>/', views.ProductDetailView.as_view(), name='product-detail'),
path('categories/<category>/', views.ProductListView.as_view(), name='product-list'),
]
Then for /categories/1, it will match with <int:pk> as 1, and thus fire the ProductDetailView.

PasswordChangeView not redirecting to PasswordChangeDoneView (or changing the password)

I am trying to use the Django authentication views. My login/logout views are working without a problem.
urls.py:
from django.urls import path
from django.contrib.auth import views as auth_views
from account import views
urlpatterns = [
path('', views.dashboard, name = 'dashboard'),
path('login/', auth_views.LoginView.as_view(), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(), name = 'logout'),
path('password_change', auth_views.PasswordChangeView.as_view(), name = 'password_change'),
path('password_change/done', auth_views.PasswordChangeDoneView.as_view(), name = 'password_change_done'),
]
password_change_form.html:
{% block body %}
<h1>Change your password</h1>
<p>Please use the following form to change your password:</p>
<div class = 'password-change-form'>
<form class="" action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" name="" value="Change">
</form>
</div>
{% endblock %}
password_change_done.html:
{% block body %}
<h1>Password Changed Successfully</h1>
{% endblock %}
I can navigate to /password_change and see the form, but when I fill it in and submit it in I am redirected to /login (I have made sure I was already logged in) and the password does not change. Does anyone know what the issue is?
This is because you specified {% url 'login' %} for the action="…" parameter, you need to change it with:
{% block body %}
<h1>Change your password</h1>
<p>Please use the following form to change your password:</p>
<div class = 'password-change-form'>
<form class="" action="{% url 'password_change' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" name="" value="Change">
</form>
</div>
{% endblock %}

why I can't get the PasswordResetDoneView page?

when I submit my Email form from PasswordResetView page the email appears via URL as following: http://127.0.0.1:8000/account/password-change/?email=medoabdin%40gmail.com and not directing me to PasswordResetDoneView page and I get no error.
how can I show PasswordResetDoneView message page
urls.py
from django.urls import path
from . import views
from django.contrib.auth.views import (
LoginView,
LogoutView,
PasswordResetView,
PasswordResetDoneView)
from django.urls import reverse_lazy
app_name = 'account'
urlpatterns = [
# /account/login/
path('login/', LoginView.as_view(template_name='account/login.html'), name='login'),
# /account/logout/
path('logout/', LogoutView.as_view(template_name='account/logout.html'), name='logout'),
# /account/register/
path('register/', views.register, name='register'),
# /account/view-profile/
path('view-profile/', views.view_profile, name='view_profile'),
# /account/password-change/
path('password-change/', PasswordResetView.as_view(template_name='account/password_change_view.html', success_url=reverse_lazy('account:password_change_done'), email_template_name='account/reset_password_email.html'), name='password_change'),
# /account/password-change/done/
path('password-chane/done/', PasswordResetDoneView.as_view(template_name='account/password_change_done.html'), name='password_change_done'),
]
password_change_done.html
{% extends 'base.html' %}
{% block title %} Success Message {% endblock %}
{% block body %}
{% if not user.is_authenticated %}
<div class="password-change-done">
<div class="container">
<h1 class="text-primary"> The Password Has Been Sent </h1>
<p class="lead text-success"> Check your email and following the rest of record until you can go back your own email. </p>
<p class="lead text-success"> You need to follow the instructions to get what you want. </p>
</div>
</div>
{% endif %}
{% endblock %}
password_change_view.html
{% extends 'base.html' %}
{% block title %} Chnage your Passowrd {% endblock %}
{% block body %}
{% if not user.is_authenticated %}
<div class="password-change">
<div class="container">
<div class="my-form">
<form class="post">
{{ form.as_p }}
<button type="submit" class="btn btn-success">Go</button>
</form>
</div>
</div>
</div>
{% endif %}
{% endblock %}
settings.py
urlpatterns = [
path('', include('index.urls')),
path('account/', include('account.urls')),
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
]
Put method="post" in form tag and also {% csrf_token %}, probably because of GET method same view is rendering and form_valid is not invoking.

How to have user log in and registration on same page using django

Currently I have a user log in page and a user sign up page, how can I have both of these on one single page?
Base.html:
<!doctype html>
<head>
{% block head %}
<title>base</title>
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
signup.html:
{% extends 'core/base.html' %}
{% block head %}
<title> Sign Up</title>
{% endblock %}
{% block body %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
{% endblock %}
login.html:
{% extends 'core/base.html' %}
{% block head %}
<title> Login</title>
{% endblock %}
{% block body %}
<h1>Login</h1>
<form method = 'post'>
{% csrf_token %}
{{ form.as_p }} <!--'form' comes from login view imported in urls-->
<button type = 'submit'>Login</button>
</form>
{% endblock %}
urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.views import login
from core import views as core_views
urlpatterns = [
url(r'^$', core_views.login_redirect, name = 'login_redirect'),
url(r'^admin/', admin.site.urls),
url(r'^login/$', login, {'template_name': 'core/login.html'}, name='login'),
url(r'^signup/$', core_views.signup, name='signup'),
url(r'^account/$', core_views.account_page, name = 'account_page')
]
views.py:
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
def login_redirect(request):
return redirect('login')
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/account')
else:
form = UserCreationForm()
args = {'form': form}
return render(request, 'core/signup.html', args)
def account_page(request):
return HttpResponse('success')
How would I put both the log in and sign up on one page, if they are handled by separate views? Thank you in advance for your response! I have no more details to add and it is making me add more details i apologize for this unnecessary text.
In any scenario where you need multiple forms in the same page the following technique can be applied.
For example currently you need two forms 'Sign In' and 'Sign Up' on the same page.
index.html
<!-- Sign In Form -->
<form>
<button type='submit' name='submit' value='sign_in'></button>
</form>
<!-- Sign Up Form -->
<form>
<button type='submit' name='submit' value='sign_up'></button>
</form>
views.py
def index(request):
if request.method == "POST":
if request.POST.get('submit') == 'sign_in':
# your sign in logic goes here
elif request.POST.get('submit') == 'sign_up':
# your sign up logic goes here

Django Login App Tutorial problems

I am having problems running through a tutorial and it seems the problems stem from this:
(r'^l/login/$', 'django.contrib.auth.views.login'),
It seems I have done all correct, but the forms dont show.
If I hit Login. I get back to the same page without forms.
Did I miss something?
Here the code:
urls.py:
from django.conf.urls.defaults import *
from formsapp.views import *
from login.views import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
#Forms1:
(r'^$', main_page),
#Forms2
(r'^register/', main_page1),
#FormsLogin:
(r'^l/', login_main_page),
(r'^l/login/$', 'django.contrib.auth.views.login'),
(r'^l/logout/$', 'logout_page'),
views.py:
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.template import RequestContext
from django.shortcuts import render_to_response
def login_main_page(request):
return render_to_response('mainpage.html', RequestContext(request))
def logout_page(request):
logout(request)
return HttpResponseRedirect('/l/')
base.html:
<html>
<head>
<title> {% block title %}{% endblock %}</title>
</head>
<body>
<h1>{% block head %}{% endblock %}</h1>
{% block content %}{% endblock %}
<br>
<br>
<br>
Login Main Page
{% if user.is_authenticated %}
Log out
{% else %}
Log in
{% endif %}
</body>
</html>
login.html:
{% extends "base.html" %}
{% block title %}Log in{% endblock %}
{% block head %}Log in{% endblock %}
{% block content %}
{% if form.has_errors %}
<p> Username or password didn't work. Please enter them again </p>
{% endif %}
<form method="post" action=".">
<p><label for="id_username">Username:
</label>{{ form.username }}</p>
<p><label for="id_password">Password:
</label>{{ form.password }}</p>
<input type="hidden" name="next"
value="/l/" />
<input type="submit" value="Log in" />
</form>
{% endblock %}
URL pattern
(r'^l/', login_main_page),
will match any URL starting with l/ and it is placed before l/login so second one won't be called, you should terminate regexp with $:
(r'^l/$', login_main_page),