Я асобираюсь создать чат между учителем и учеником. При регистрации у них должно быть возможность задать себе роль Teacher или Learner Как это создать? Пожалуисто поделитесь своим мнением со мной.
Я пытался что-то сделать , но я получаю ошибки
# viewsdef register(request):
registered = False
if request.method == "POST":
user_form = RegistrationForm( request.POST)
profile_form = UserProfile( request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit= False)
profile.user = user
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
profile.save()
registered = True
return redirect('accounts/')
else:
user_form = RegistrationForm()
profile_form = UserProfile()
args = { 'user_form' : user_form, 'profile_form': profile_form }
return render(request, 'registration/reg_form.html', args)
@login_required
def home(request):
return render( request, 'registration/home.html' )
@login_required
def settings(request):
return render(request, 'registration/settings.html')
@login_required
def view_profile(request):
args = {'user': request.user}
return render(request,'registration/profile.html', args)
@login_required
def edit_profile(request):
print(request)
if request.method == "POST":
form = EditProfileForm(request.POST, instance = request.user)
if form.is_valid():
form.save()
return redirect('/accounts/profile')
form = EditProfileForm(instance = request.user)
args = {'form' : form}
return redirect(request, 'registration/edit_profile.html', args)
@login_required
def change_password(request):
print(request)
if request.method == "POST":
form = PasswordChangeForm(data = request.POST, user = request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return redirect('/accounts/profile')
form = PasswordChangeForm(user = request.user)
args = {'form' : form}
return redirect(request, 'registration/change_password.html', args)
#urls.py from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
#Password_ResetView, Password_Reset_DoneView
urlpatterns = [
path('', views.home, name= 'home'),
path('register/', views.register, name= 'register'),
path('login/', auth_views.LoginView.as_view(template_name='registration/login.html'), name= 'login'),
path('logout/', auth_views.LogoutView.as_view(template_name='registration/logout.html'), name= 'logout'),
path('profile/', views.view_profile, name= 'views_profile'),
path('profile/edit', views.edit_profile, name= 'edit_profile'),
path('change-password/', auth_views.PasswordChangeView.as_view(template_name='registration/change_password.html'), name= 'change_password'),
path('password_reset/', auth_views.PasswordResetView.as_view(),
{ 'template_name':'registration/password_reset.html', 'post_reset_redirect': 'password_reset_done',
'email_tamplate_name': 'registration/password_reset_email.html' }, name= 'password_reset'),
path('change-password/done/', auth_views.PasswordChangeDoneView.as_view(template_name='registration/change_password_done.html'), name= 'change_password_done'),
path('reset-password/done/', auth_views.PasswordResetDoneView.as_view(template_name='registration/password_reset_done.html'), name= 'password_reset_done'),
path('reset-password/confirm<uidb64>/<token>', auth_views.PasswordResetConfirmView
.as_view(template_name='registration/password_reset_confirm.html'), name= 'password_reset_confirm'),
path('reset-password/complete', auth_views.PasswordResetCompleteView
.as_view(template_name= 'registration/password_reset_complete.html'), name= 'password_reset_complete'),
path('settings', views.settings, name= 'settings'),
]
# models.pyfrom django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
avatar = models.ImageField(upload_to = 'profile_pics', blank = True)
def create_profile(sender,**kwargs):
if kwargs ['created']:
user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender= User)
def __str__(self):
return self.mdels.user.username
#forms.pyfrom django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from accounts import models
class RegistrationForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email','password1')
def save(self , commit = True):
user = super(RegistrationForm, self).save(commit= False)
user.last_name = self.cleaned_data['last_name']
user.last_name = self.cleaned_data['first_name']
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
class UserProfile(forms.ModelForm):
role = forms.ChoiceField(widget = forms.RadioSelect(),choices = ([('teacher','Teacher'), ('learner','Learner')]))
class Meta():
model = models.UserProfile
fields = ("avatar",)
def save(self , commit = True):
user_profile = UserProfile
user_profile.avatar = self.cleaned_data['avatar']
user_profile.role = self.cleaned_data['role']
class EditProfileForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'password')