I'm trying to make register possible on the homepage (register box appears onclick), so I don't have a seperate URL to handle registration. I'm trying to send the form through get_context_data, however it's not working. Here's my code:
forms.py
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
confirm_password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'username',
'password',
'confirm_password',
]
views.py
class BoxesView(ListView):
template_name = 'polls.html'
def get_context_data(self):
context = super(BoxesView, self).get_context_data()
# login
form = UserRegistrationForm(self.request.POST or None)
context['form'] = form
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = User.objects.create_user(username, password)
user.save()
return redirect('/')
return context
return context
def get_queryset(self):
pass
base.html
<form action="" enctype="multipart/form-data" method="post">{% csrf_token %}
<div class="registerBox">
{{ form.username }}
{{ form.password }}
<input type="submit" value="register"/>
</div>
</form>
The fields show up but after submitting the form it doesn't create a User because my form.is_valid is False. Any idea?
You shouldn't return a response from get_context_data(). Instead, do that in the post() method like this:
class BoxesView(ListView):
template_name = 'polls.html'
def post(self, request, *args, **kwargs):
form = UserRegistrationForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = User.objects.create_user(username, password=password)
return redirect('/')
else:
return self.get(request, *args, **kwargs)
def get_context_data(self):
context = super(BoxesView, self).get_context_data()
context['form'] = UserRegistrationForm()
return context
Looks like your Form expects to have confirm_password submitted, but that's not part of your html form.
Related
I am entirely new to django.
Trying to create login and registration system.
User registers successfully, saved in database but I get this error after registering.
"'NoneType' object has no attribute 'set_password'"
views.py
def register(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
form.save()
username = form.cleaned_data.get('username')
fullname = form.cleaned_data.get('fullname')
password = form.cleaned_data.get('password1')
user = authenticate(request, username=username, password=password)
messages.success(request, f'Welcome to blaza {fullname}')
user.set_password(password)
user.save()
if user is not None:
login(request, user)
return redirect(reverse('home'))
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form': form})
When I remove "user.set_password" it works but registered users can not login with their credentials even when the username and password is correct, It says incorrect username and password. (only admin account, superuser can login).
So I researched and had to add the user.set_password and user = form.save (I get warning that local variable user value is not used)
forms.py
class SignUpForm(UserCreationForm):
username = forms.CharField(max_length=50)
fullname = forms.CharField(max_length=200)
email = forms.EmailField(max_length=200)
password2 = None
class Meta:
model = User
fields = ('username', 'fullname', 'email', 'password1')
def clean_password1(self):
password1 = self.cleaned_data.get('password1')
try:
password_validation.validate_password(password1, self.instance)
except forms.ValidationError as error:
self.add_error('password1', error)
return password1
Models.py
class CustomUser(AbstractUser):
class Meta:
db_table = 'users'
fullname = models.CharField(max_length=200)
email = models.EmailField(max_length=150)
profile_photo = models.ImageField(upload_to='images/profile_pics', default='images/nophoto.png')
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
message="Phone number must be entered in the format: '+999999999'. Up to 15 "
"digits "
"allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=13, default='')
address = models.CharField(max_length=100, default='')
has_store = models.BooleanField(default=False)
signup.html
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('accounts/', include('users.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
Or Someone help me with a secured and working registration and login that actually works.
In a simple signup form:
def register(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
fullname = form.cleaned_data.get('fullname')
password = form.cleaned_data.get('password1')
saved_user = form.save(commit=False)
saved_user.set_password(password)
saved_user.save()
user = authenticate(request, username=username, password=password)
messages.success(request, f'Welcome to blaza {fullname}')
if user is not None:
login(request, user)
return redirect(reverse('home'))
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form': form})
The reason you get noneType because you save user after the authenticate() so it return None
I try to find a similar question but I did not find the answer what I want. I am new to Django, I was trying to learn about authentication in Django but I got an error like this:
AttributeError: 'AnonymousUser' object has no attribute '_meta'
Here is my code:
views.py
def login(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = request.GET['username']
password = request.GET['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
return redirect('/')
else:
error = " Sorry! Username and Password didn't match, Please try again ! "
return render(request, 'girl/login.html',{'error':error})
else:
form = LoginForm()
return render(request, 'girl/login.html', {"form":form})
forms.py
class LoginForm(forms.ModelForm):
class Meta:
model = User
fields = ('username', 'password')
login.html
{% extends 'base.html' %}
{% block content %}
<h1>Login</h1>
{% if error %}
{{ error }}
{% endif %}
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="login">
</form>
{% endblock %}
Any help would be appreciated. Thanks!
You just need to inherit from forms.Form not forms.ModelForm,
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput())
Also, in your views, edit something like this,
def login(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
return redirect('/')
else:
error = " Sorry! Username and Password didn't match, Please try again ! "
else:
form = LoginForm()
return render(request, 'girl/login.html', {"form":form})
You set password as a normal text. Try like this.
def login(request):
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
# normalized data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
return redirect('/')
else:
error = " Sorry! Username and Password didn't match, Please try again ! "
return render(request, 'girl/login.html',{'error':error})
else:
form = LoginForm()
return render(request, 'girl/login.html', {"form":form})
I'm trying to make form and method which allows users to change their password. When user type their password into two inputs than form will check if those are same and update db. However I got this error and I couldn't solve it.
error message
NoReverseMatch at /blog/password_change/blue/
Reverse for 'profile' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'blog/profile/(?P<username>[-\\w.]+)/$']
urls.py
url(r'^password_change/(?P<username>[-\w.]+)/$', views.password_change, name='password_change'),
url(r'^profile/(?P<username>[-\w.]+)/$', views.profile, name='profile'),
views.py
def password_change(request, username):
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(reverse('blog:profile'))
else:
return redirect(reverse('blog:profile'))
else:
form = PasswordChangeForm(user=request.user)
return HttpResponseRedirect('/blog/')
profile.html. this is the template.
<form class="form-horizontal" role="form" action="{% url'blog:password_change' user.username %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<label for="password1">new password</label>
<div class="row">
<input type="password" name="password1" id="password1"/></div>
<div class="row">
<label for="password2">password check</label></div>
<div class="row">
<input type="password" name="password2" id="password2"/></div>
<div class="row">
<button type="submit" class="button-primary">change password</button></div>
views.py
def password_change(request, username):
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(reverse('blog:profile'))
else:
return redirect(reverse('blog:profile'))
else:
form = PasswordChangeForm(user=request.user)
return HttpResponseRedirect('/blog/')
This is forms.py
class PasswordChangeForm(forms.ModelForm):
error_messages = {
'password_mismatch': ("The two password fields didn't match."),
}
password1 = forms.CharField(
label=("Password"),
strip=False,
widget=forms.PasswordInput,
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=("Enter the same password as before, for verification."),
)
class Meta:
model = User
fields = ("username",)
field_classes = {'username': UsernameField}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True})
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
self.instance.username = self.cleaned_data.get('username')
password_validation.validate_password(self.cleaned_data.get('password2'), self.instance)
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
As your error states, you need an argument for your username in profile url, which you don't provide at all.
For example:
return redirect(reverse('blog:profile'))
should be
return redirect(reverse('blog:profile', args=[form.user.get_username()]))
You must pass username to reverse().
username = form.user.get_username()
reverse('blog:profile', kwargs={'username':username})
I want to fetch each and every detail of user profile. I know function get_profile() has been depreciated.
I want get user profile and then pass it as context to template.
Actually I am making a "Edit user profile" functionality.
My Model:
class UserProfile(models.Model):
user = models.OneToOneField(User)
state = models.CharField(max_length=200, blank=True)
country = models.CharField(max_length=200, blank=True)
zipcode = models.CharField(max_length=200, blank=True)
And views:
#login_required
def dashboard(request):
context = RequestContext(request)
profile = request.user.userprofile
context_dict = {'profile': profile}
return render_to_response('appname/dashboard.html', context_dict, context)
To edit both User and Profile instance you have to use two forms:
class UserForm(forms.ModelForm):
class Meta:
class = User
fields = ('username', 'first_name', 'last_name')
class ProfileForm(forms.ModelForm):
class Meta:
class = UserProfile
exclude = ('user', )
#login_required
def edit_profile(request):
user = request.user
profile = user.userprofile
if request.method == 'POST':
user_form = UserForm(request.POST, instance=user)
profile_form = ProfileForm(request.POST, instance=profile)
if all([user_form.is_valid(), profile_form.is_valid()]):
user_form.save()
profile_form.save()
return redirect('.')
else:
user_form = UserForm(instance=user)
profile_form = ProfileForm(instance=profile)
return render(request, 'user_profile.html',
{'user_form': user_form, 'profile_form': profile_form})
And the template should contain both forms in the single <form> tag:
<form action="." method="POST">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<button>Update</button>
</form>
I have tried all the solutions given on SO but was not able to slve this
view.py
def signup(request):
form = SignupForm(request.GET)
print("%s"%request.GET['hobby'])
form.errors
#h=SignupForm(request.POST)
if form.is_valid():
email = request.GET['email']
location = request.GET['location'] users=User(username=username,email=email,password=request.GET['password'],location=location)
user_profile = request.user.profile
user_profile.location = location
user_profile.save()
form.save()
return HttpResponseRedirect('mtweet/')
return render(request,'mtweet/signup.html',{'SignupForm':form})
form.py
class SignupForm(UserCreationForm):
username=forms.CharField(label = " Username",required=True)
email = forms.EmailField(label = "Email",required=True)
password = forms.CharField(widget = forms.PasswordInput,required=True)
location=forms.CharField(label="Location",required=False)
class Meta:
model = User
fields = ("username", "email","location")
signup.html
<div id="register">
<form method="post" action="{% url 'mtweet.views.signup' %}">
{% csrf_token %}
<table>
{{ SignupForm.as_p}}
</table>
<input type="submit" value="Submit" />
</form>
</div>
There are a few problems with your code; let me try and re-write it:
class SignupForm(forms.Form):
username=forms.CharField(label = " Username",required=True)
email = forms.EmailField(label = "Email",required=True)
password = forms.CharField(widget=forms.PasswordInput,required=True)
location=forms.CharField(label="Location",required=False)
def signup(request):
form = SignupForm(request.POST, request.FILES)
if form.is_valid():
email = form.cleaned_data['email']
location = form.cleaned_data['location']
password = form.cleaned_data['password']
username = form.cleaned_data['username']
user = User.objects.create_user(username, email, password)
user.save()
user_profile = user.profile
user_profile.location = location
user_profile.save()
return HttpResponseRedirect('mtweet/')
else:
return render(request,'mtweet/signup.html',{'SignupForm':form})
return render(request,'mtweet/signup.html',{'SignupForm':SignupForm()})