1

This is what my registration form looks like:

Registration form

I want to make it so that if the user doesn't write anything in the given text boxes, a warning sign pops up and say that ' a username must be inputted' or something like that

but when i click the submit button, Value error occurs, saying that "The given username must be set"

def register(request): context = { 'error_message': None, } if request.method == 'POST': username = request.POST['username'] password1 = request.POST['password1'] password2 = request.POST['password2'] email = request.POST['email'] if password1==password2: if User.objects.filter(username=username).exists() or Person.objects.filter(username=username).exists(): context['error_message'] = '이미 사용중인 아이디입니다.' return render(request, 'UserAdministration/register.html', context) elif User.objects.filter(email=email).exists() or Person.objects.filter(username=username).exists(): context['error_message'] = '이미 사용중인 이메일입니다.' return render(request, 'UserAdministration/register.html ', context) else: user = User.objects.create_user( password=password1, email=email, username=username ) user.save() masked_username = generate_masked_username.generate_masked_username(username) person = Person.objects.create( username=username, masked_username=masked_username, email=email, password=password1 ) person.save() return redirect('login') else: context['error_message'] = '비밀번호가 맞지 않습니다.' return render(request, 'UserAdministration/register.html', context) # return redirect('/') # originally was homepage.html. Doesn't know if this changed anything. just a note else: return render(request, 'UserAdministration/register.html', context) 

This is my code... anybody know how to prevent this error?

1 Answer 1

1

This is what forms are for. You should use a Form (or ModelForm) rather than building the form and using request.POST manually. Then you will get better errors. These docs will be especially useful.

A quick example:

Form:

from django import forms from django.contrib.auth import get_user_model class RegistrationForm(forms.ModelForm): class Meta: model = get_user_model() fields = ["username", "email", "password"] 

View:

from django.urls import reverse_lazy from django.views.generic.edit import CreateView from .forms import RegistrationForm class UserRegistrationView(CreateView): form_class = RegistrationForm template_name = "registration/register.html" success_url = reverse_lazy("login") 

This will get you most of the way there, you just need to add a custom field and some logic for the password2 validation and handle whatever the masked username stuff is.

Registration is such a common workflow however, that you can also use a package like django-registration to handle the details for you, and you can just override what you need for your custom logic where it's needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment appreciate it bud

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.