I want to save a username in database, but it doesn't work. Below is the code. I've created a DB named User and I'm trying to save the user who has been taking by the input from the html I've created. And my database User contains only username field. I used the UserCrationForm as default, and my database appear as polls_user in phpmyadmin. I don't think that's the problem. When I'm trying to save from shell, its working, using the save modelform.
views.py
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
q = form.save(commit=False)
q.username = request.username
q.save()
return HttpResponseRedirect('/polls/login')
else:
messages.error(request, "Error")
args = {}
args.update(csrf(request))
args['form'] = UserCreationForm()
return render_to_response('polls/register.html', args, context_instance= RequestContext(request))
register.html
{% block content %}
<h2> Register</h2>
<form action="/polls/login/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Register">
</form>
{% endblock %}
I cant see where the problem is.
Someone help?
Problem is here: q.username = request.username .It should be:
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
q = User() #import your own User model here.
q.username = request.user.username
q.save()
return HttpResponseRedirect('/polls/login')
PS: This will only get you the username of the logged in user.
EDIT:
(from comments)
As you can see, UserCreationForm is a modelform, where in meta class, model is auth.User model(check here). So your data is being saved in auth.User. So override the UserCreationForm like this (If you want to use the UserCreationForm):
#form
class MyUserCreationForm(UserCreationForm):
class Meta:
model = PollUser #Your poll user model
fields = ("username",)
#view
if request.method == 'POST':
form = MyUserCreationForm(request.POST)
if form.is_valid():
....
For this solution, you have to keep in mind that, you need to subclass PollUser model from auth.User model. If you want to use your own PollUser model (not subclassing from auth.User model), then I will suggest you to make a new modelform for PollUser and not use UserCreationForm.
Related
I am trying to save users IP address in my extended profile model. The goal is to make this a hidden field. Currently, I can debug by printing the IP address to the console. The issue arises when I try and save the info.
views.py
def index(request):
#request.session.flush()
if request.user.is_authenticated:
return redirect('ve:dashboard')
elif request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.refresh_from_db() # Load the profile instance created by the Signal
user.profile.birth_date = form.cleaned_data.get('birth_date')
user.ipaddress = get_ip(request)
print(user.ipaddress)
user.save()
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=user.username, password=raw_password)
login(request, user)
return redirect('ve:dashboard')
else:
form = RegistrationForm()
return render(request, 'index.html', {'form': form})
forms.py
class RegistrationForm(UserCreationForm):
# birth_date = forms.DateField(help_text='Required. Format: YYYY-MM-DD')
birth_date = forms.DateField(widget=SelectDateWidget(years=range(1999, 1910, -1)))
#ipaddress = forms.IntegerField(widget=forms.HiddenInput(), required=False)
class Meta:
model = User
fields = ('username', 'email', 'birth_date', 'password1', 'password2',)
exclude = ['ipaddress',]
index.html
<form method="post">
{% csrf_token %}
{% for field in form %}
<p class="text-left">
{{ field.label_tag }}<br>
{{ field }}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
...
ipaddress = models.CharField(default="0.0.0.0", max_length=30)
This form was working fine before I tried adding the ipaddress field. I've been trying several versions and sometimes the form creates a new user but the ipaddress is not saved..
The current code above gives me there error on POST:
DoesNotExist at / User matching query does not exist. Due to this line "user.refresh_from_db() # Load the profile instance created by the Signal"
From the docs:
This save() method accepts an optional commit keyword argument, which accepts either True or False. If you call save() with commit=False, then it will return an object that hasn’t yet been saved to the database.
So since you're passing commit in as False you're getting an unsaved instance back. Attempting to call refresh_from_db on an object that doesn't actually exist in the database will fail, as it is clearly doing. If the instance to a model has no id then refresh_from_db will fail when called on it.
As for the continuing inability to save IP address, I noticed that your form meta has the model set to the User object. The default Django User object has no ip address. I see that in the model file you linked you have a Profile model that does have an IP Address so in that case I think you simply have your form set up wrong. Or you need to handle the request differently.
Form change
Currently your form is attempting to create/modify a Django User model. Unless you've made a custom User model that you didn't show, this user model will not have an ipaddress as a field in the database meaning even if you set user.ipaddress = <address> and then save the user, the ip address won't persist outside of the current scope since all you did was declare a new variable for the user instance.
If you change your form to point at your Profile model you'll be able to save the address using profile.ipaddress = <address> and save it successfully. But you will have to update your template since by default it will only show the fields for your profile and not the user object associated with it.
Change Template/View
You can also change the template and view to accommodate it. Apparently your view is able to produce an IP Address using the get_ip function so for the time being I'll assume your template is fine as is so the only changes that need to be made are to your view.
Currently your view is getting an unsaved User instance back when it calls form.save. This means you need to save the user and then create a Profile model that references it with your ip address attached.
def index(request):
#request.session.flush()
if request.user.is_authenticated:
return redirect('ve:dashboard')
elif request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
# do anything you need to the unsaved user here
user.save()
prof = Profile.objects.create(user=user,
ipaddress=get_ip(request),
date=form.cleaned_data.get('birth_date')
# no need to save prof since we called objects.create
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=user.username, password=raw_password)
login(request, user)
return redirect('ve:dashboard')
else:
form = RegistrationForm()
return render(request, 'index.html', {'form': form})
Python noob here trying to learn something very simple.
I'm trying to create a basic form that takes some personal information from a user and saves it into a sqlite3 table with the username of the user as the primary key.
My models.py looks like this:
class userinfo(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key= True,
on_delete=models.CASCADE)
name = models.CharField(max_length = 200, blank = True)
email = models.EmailField(max_length= 300, default = 'Null')
phone = models.CharField(max_length= 10, default = 'Null')
def __unicode__(self):
return self.user
forms.py:
class NewList(forms.ModelForm):
class Meta:
model = userinfo
exclude = {'user'}
views.py
def newlist(request):
if request.method == 'POST':
form = NewList(request.POST)
if form.is_valid():
Event = form.save(commit = False)
Event.save()
return redirect('/home')
else:
form = NewList()
return render(request, 'home/newlist.html', {'form': form})
html:
{% load static %}
<form action="/home/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
urls.py too, but I don't know how that would help:
urlpatterns = [
url(r'^newlist/$', views.newlist, name='newlist')
]
So when I go to the url, I see the form. I can then fill the form, but when I submit the form, the data doesn't go into the database.
What am I doing wrong here?
Thanks in advance!
I think all you need to do is just save the form if it's valid, probably also add the userinfo as an instance of the form. You are also exluding the user from the form and need to assign it manually.
def newlist(request):
if request.user.is_authenticated():
user = request.user
if request.method == 'POST':
form = NewList(request.POST, instance=user.userinfo)
if form.is_valid():
form.save(commit=false)
form.user = user
form.save()
return redirect('/home')
else:
form = NewList(instance=user.userinfo) # add this if you want it to automatically fill the form with the old data if there is any.
return render(request, 'home/newlist.html', {'form': form})
The rest look like it should work.Except you need to send the post URL back to newlist:
{% load static %}
<form action="/newlist/" method="POST">
{% csrf_token %}
{{ form.as_p }}
</form>
If users are assigned at the creation of the model the first time, you don't need the user save, but since this is saving a users data you want to make sure they are logged in anyway:
def newlist(request):
if request.user.is_authenticated():
user = request.user
if request.method == 'POST':
form = NewList(request.POST, instance=user.userinfo)
if form.is_valid():
form.save()
return redirect('/home')
else:
form = NewList(instance=user.userinfo) # add this if you want it to automatically fill the form with the old data if there is any.
return render(request, 'home/newlist.html', {'form': form})
The instance is the model it is either going to save to, or copying data from. In the: form = NewList(request.POST, instance=user.userinfo) part, it is taking the POST data from the form, and linking that to the specific model entry of user.userinfo, however, it will only save to the database when you call form.save().
The user.userinfo is just so you can get the correct form to save to, as userinfo is a onetoone model to user. Thus making it possible to get it with user.userinfo.
The form = NewList(instance=user.userinfo) part is where it takes the currently logged in user's userinfo and copies into the form before it is rendered, so the old userinfo data will be prefilled into the form in the html. That being if it got the correct userinfo model.
I've already enabled the ability for users to create profiles; however, I need to be able to save additional data to the profile once the user has already logged in.
Say that John is logged in. After he logs in, he decides that he wants to bookmark a certain term. Every time that he bookmarks a term (the url of the current page), it will be added to the "terms" section associated with his profile.
I'm not sure how to allow for the addition of more than one term - don't think CharField is correct, and I also don't know how to link the form in the template to the view so that it actually saves this data to the profile. Here is what I currently have:
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
terms = models.CharField(max_length=1000)
views.py
This is how I created my user:
def register(request):
context = RequestContext(request)
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=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
profile.save()
registered = True
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render_to_response(
'app/register.html',
{'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
context)
And this is what I'm attempting to do with terms:
def terms(request):
context = RequestContext(request)
url = request.get_full_path()
profile = UserProfile()
profile.topics = url
profile.save()
return render_to_response('app/page1.html', {}, context)
urls.py
url(r'^terms/$', views.terms, name='terms'),
And the template file.
<form method="post" action="{% url 'terms' %}" enctype="multipart/form-data">
{% csrf_token %}
<input type="submit" name="submit" value="Save" />
</form>
I'm think you can use https://github.com/brosner/django-bookmarks to save bookmarks for any user without your code.
Or add model smt about Term like this
class Term(models.Model):
user = models.ManyToManyField(User, related_name='terms')
term = models.CharField(max_length=1000)
and use user.terms.all() for get all user's terms.
But, if you need save your schema and models, you need custom field which will works like array field in postgres. If you use postgresql db, add https://github.com/ecometrica/django-dbarray to you project and use terms like TextArrayField, if you db is not postgres see here -
What is the most efficent way to store a list in the Django models?
second answer titled "Premature optimization is the root of all evil." by user jb.
Solution with relations is "normalized" - simple and strong
Solution with "array-field" is "demormalized" - faster but with risks of non-consistently data.
I am getting a valid response back when requesting my form, but I am getting no form fields with the response. It is loading the Submit button only, but no form fields.
Goal: get form fields to load and be able to submit form.
I have a views.py:
def Registration(request):
form = NewUserRegistrationForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form.save()
return HttpResponseRedirect("/Login/")
else:
form = NewUserRegistrationForm()
return render(request, 'VA/reuse/register.html', {
'form': form
})
forms.py
class NewUserRegistrationForm(UserCreationForm):
username = forms.CharField(required=True,max_length=30,validators=[RegexValidator('^[A-Za-z0-9]{1,30}$','e.g. must be 30 characters or less','Invalid Entry')])
email = forms.EmailField(required=True, max_length=75)
password = forms.PasswordInput()
class Meta:
model = User
fields = ("username", "email", "password1","password2")
def save(self, commit=True):
user = super(NewUserRegistrationForm, self).save(commit=False)
user.username = self.cleaned_data["username"]
user.email = self.cleaned_data["email"]
user.password = self.cleaned_data["password1"]
if commit:
user.save()
return user
a template
<div id="register_bubble">
<form method="post" id="userRegistration">
{% csrf_token %}
{{ NewUserRegForm.as_p }}
<input type="submit" value="Submit" />
</form> <!-- /RegistrationForm (FORM) -->
</div>
What am I doing wrong here? I'm getting no error while in debug mode locally either.
Thank you!
You have two mistakes.
Firstly, you're passing the form class into the template context, not the form instance: the class is NewUserRegistrationForm, but you've instantiated it as NewUserRegForm, and that's what you should be passing as the value in the form context.
To make it more complicated, the key name you've given to that value is also NewUserRegistrationForm - but you're still referring to NewUserRegForm in the template, even though that doesn't exist there.
This would be much more obvious if you used PEP8 compliant names. Instances should be lower case with underscore: eg new_user_registration_form. However, in this case you could just call it form, since there's only one.
return render(request, 'mysite/reuse/register.html', {
'NewUserRegForm': NewUserRegForm
})
or, better:
form = NewUserRegistrationForm(request.POST or None)
...
return render(request, 'mysite/reuse/register.html', {
'form': form
})
You're passing the form instance to the context as 'form', but calling it in the template as {{ NewUserRegForm.as_p }}.
You should use {{ form.as_p }} instead.
The Problem:
I'm tying to post to a view and pass on a value from the template by using a hidden value field and a submit button. The values from the submit button (ie the csrf_token) gets through but the hidden value does not. I've checked from the Wezkrug debugger that request.POST only contains form values and not my 'id' value from the hidden field.
Background:
The button takes you to a form where you can enter a comment. I'm trying to include the review.id that the user is commenting on to make commenting easy. I have the value as 'test' not for test purposes.
My form:
<div>
<form method='POST' action='/add_comment/'>
{% csrf_token %}
<input type="hidden" name='id' value='test'>
<input type="submit" value="Make a Comment">
</form>
</div>
Comment View:
#login_required
def make_comment(request):
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.user = request.user
comment.save()
# render?
return HttpResponseRedirect('/results/', {
'restaurant': get_object_or_404(
Restaurant,
name=request.POST['name'],
address=request.POST['address']
)
})
else:
form = CommentForm()
return render(request, 'stamped/comment.html', {'form': form})
Comment Model:
class Comment(models.Model):
content = models.TextField()
review = models.ForeignKey(Review)
user = models.ForeignKey(User)
date_added = models.DateTimeField(auto_now_add=True)
Comment ModelForm Code:
class CommentForm(ModelForm):
class Meta:
model = Comment
exclude = ('user', 'review',)
I've been trying to follow the tactics in this question, but using the request.session dict is undesirable because Id have to store an id for every review regardless if they're are ever commented on.
What is a more efficient way to pass variables from Template to View in Django?
Any ideas on how to include the hidden value in the POST? Thanks!
views.py
def make_comment(request):
if request.method == 'POST':
if 'prepair_comment' in request.POST:
review = get_object_or_404(Review, pk=request.POST.get('id'))
form = CommentForm({'review': review.id})
return render(request, 'stamped/comment.html', {
'form': form,
})
else: # save the comment
models.py
class CommentForm(ModelForm):
class Meta:
model = Comment
exclude = ('user',)
widgets = {'review': forms.HiddenInput()}
restaurant.html
<form method='POST' action='/add_comment/'>
{% csrf_token %}
<input type='hidden' value='{{ r.id }}' name='id'>
<input type="submit" name='prepair_comment' value="Make a Comment">
</form>
You can access the form with form.cleaned_data. You could also use a if form.is_valid() or if you want to ignore the hidden test value when there is no comment, then you could use a if/else logic to ignore the hidden value if comment is None: logic.
To access the form and only record the test value if comment is not None, the views.py might look like this:
def form_view(request):
if request.method == 'POST'
form = form(request.POST)
if form.is_valid():
comment = form.cleaned_data['comment']
# do something with other fields
if comment is not None:
id = form.cleaned_data['test']
# do something with the hidden 'id' test value from the form
return HttpResponseRedirect('/thanks/')
else:
form = form()
return render(request, 'form.html', {'form': form})
Here are the Django Docs that I would reference for this:
https://docs.djangoproject.com/en/dev/topics/forms/