@ool3

Как правильно написать форму в django?

Одна и таже ошибка со всеми формами!
5ef9c73e93d7d006399539.png

Views.py
from django.shortcuts import render
from django.views import generic
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordChangeForm
from django.urls import reverse_lazy
from .forms import SignUpForm, EditUpForm, PasswordChangingForm
from django.contrib.auth.views import PasswordChangeView

class PasswordsChangeView(PasswordChangeView):
	form_class = PasswordChangingForm
	success_url = reverse_lazy('password_success')
	template_name = 'registration/change_password.html'

def password_success(request):
	return render(request, 'registration/password_success.html', {})
	
class UserRegisterView(generic.CreateView):
    form_class = SignUpForm
    template_name = 'registration/register.html'
    success_url = reverse_lazy('login')


class UserEditView(generic.UpdateView):
	form_class = UserChangeForm
	template_name = 'registration/profile.html'
	success_url = reverse_lazy('home')
	
	def get_object(self):
		return self.request.user


Urls.py
from django.urls import path, include
from .views import UserRegisterView, UserEditView, PasswordsChangeView
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
    path('register/', UserRegisterView.as_view(), name='register'),
    path('edit_profile/', UserEditView.as_view(), name='edit_profile'),
    # path('password/', auth_views.PasswordChangeView.as_view(template_name='registration/change_password.html')),
    path('password/', PasswordsChangeView.as_view()),
    path('password_success', views.password_success, name='password_success')
]


Forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordChangeForm
from django.contrib.auth.models import User
from django import forms

def SignUpForm(UserCreationForm):
	email = forms.EmailField( widget=forms.EmailInput(attrs={'class': 'form-control'}))

	class Meta:
		model = User
		fields = ('username', 'email', 'password1', 'password2')


	def __init__(self, *args, **kwargs):
		super(SignUpForm, self).__init__(*args, **kwargs)
		self.fields['username'].widget.attrs['class'] = 'form-control'
		self.fields['password1'].widget.attrs['class'] = 'form-control'
		self.fields['password2'].widget.attrs['class'] = 'form-control'


def EditUpForm(UserChangeForm):
	email = forms.EmailField()
	first_name = forms.CharField(max_length=100)
	last_name = forms.CharField(max_length=100)
	date_joined = forms.CharField(max_length=100)

	class Meta:
		model = User
		fields = ('username', 'email', 'password')


def PasswordChangingForm(PasswordChangeForm):
	old_password = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'class': 'form-control', 'type': 'passowrd'}))
	new_password1 = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'class': 'form-control', 'type': 'passowrd'}))
	new_password2 = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'class': 'form-control', 'type': 'passowrd'}))


	class Meta:
		model = User
		fields = ('old_password', 'new_password1', 'new_password2')


html код страниц (он у все страниц позожий)
{% extends 'base.html' %}
{% block title %}
Profile
{% endblock %}
{% block content %}


<h1>Edit Profile...</h1>
<br><br>
<div class="form-group">
    <form method="POST">
        {% csrf_token %}
     	{{form.as_p}}
     	<button class="btn btn-warning">Profile Update</button>
    </form>
</div>
{% endblock %}
  • Вопрос задан
  • 777 просмотров
Решения вопроса 1
tumbler
@tumbler Куратор тега Django
бекенд-разработчик на python
Вот жеж маскировка 100 уровня... Вы вместо классов форм объявили функции, вот ничего и не работает.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы