Django - Keep specific fields on form after submit - django

I have a view that has a simple "save and add another" functionality, that redirects the user to the same page after submit the form.
View:
def new_planning(request):
form = PlanningForm(request.POST)
if form.is_valid():
form.save()
if 'another' in request.POST:
messages.success(request, ('Success!'))
return redirect('new_planning')
else:
return redirect('list_planning')
return render(request, 'pages/planning/new_planning.html', context={
'form': form,
})
Form:
class PlanningForm(forms.ModelForm):
accountplan = ModelChoiceField(
queryset=AccountsPlan.objects.filter(active=True).order_by('code'),
)
month = forms.DateField(
required=True,
error_messages={'required': '', },
)
amount = forms.DecimalField(
max_digits=9,
decimal_places=2,
required=True,
validators=[
error_messages={'required': '', },
)
class Meta:
model = Planning
fields = '__all__'
The function works as expected and after the submit, the same page is rendered with a blank form. What I want is to keep just the "amount" field blank and keep the data typed in the "accountplan" and "month" fields. Is there a way to do this?
I read about instance in the docs, but it doesn't seem to be what I looking for, since I don't want to get the data from the database (if that's possible), but simply keep the last inputs typed in both fields.

If you rewrite the "ModelForm" to a "Model" class, you can get the values of the posted datas, and can be rendered to the page.
For example:
# views.py
def ContactPageView(request):
if request.method == "POST":
email = request.POST.get('email')
message = request.POST.get('message')
message_element = ContactFormObject(email=email, message=message)
message_element.save()
else:
name, message = '', ''
form_data = name, message
return render(request, 'contact.html', {'form_data': form_data})
# models.py
class ContactFormObject(models.Model):
email = models.CharField(max_length=100) #....
ModelForm is more comfortable, but I don't recommend it if you have extra 10 minutes to code some HTML in order to the possibilities of more customization.

Related

Problem with logic building for registering a model in django

I am in the middle of a project. I have extended the custom django user and modified it.
this is my user model:-
class User(AbstractUser):
name = models.CharField(max_length=200, null=True, blank=True)
usertype = models.CharField(choices = [('d','doctor'), ('p','patient')], max_length=1)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
def __str__(self):
return self.name
Also I have declared two seperate models named Patient and Doctors. My objective is to register the users in their respective models(Doctors or Patients) by checking the usertype.
Here are those models:-
class Patient(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='patient')
dob = models.DateField(null=True, blank=True)
contact = models.CharField(null=True, blank=True, max_length=100)
def __str__(self):
return self.user.name
class Doctor(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='doctor')
deg = models.TextField(null=True, blank=True)
def __str__(self):
return self.user.name
Now at the front end I want to apply the logic as every time a user is registered the user selects the usertype and based on that selection the Doctor or the Patient module is updated.
I have tried creating separate forms for that too.
Here are my forms :-
class MyUserCreation(UserCreationForm):
class Meta:
model = User
fields = ['name','username','usertype']
class DoctorCreation(ModelForm):
class Meta:
model = Doctor
fields = ['user','deg']
class PatientCreation(ModelForm):
class Meta:
model = Patient
fields = ['dob', 'contact','user']
The view handling this URL is :-
def registerUser(request):
page = 'general'
form = MyUserCreation()
if request.method == 'POST':
form = MyUserCreation(request.POST)
if form.is_valid:
user = form.save(commit=False)
user.save()
login(request, user)
return redirect('home')
else:
messages.error(request, 'Error occured')
if user.usertype == 'p':
page = 'patient'
form = PatientCreation()
form = PatientCreation(request.POST)
if form.is_valid:
form.save()
elif user.usertype== 'd':
page = 'doctor'
form = DoctorCreation()
form = DoctorCreation(request.POST)
if form.is_valid:
form.save()
context = {'form':form, 'page':page}
return render(request, 'rec/register_user.html', context )
The front end for this project is handled with very basic HTML.
Also, if possible I want the front end such that every time a user is registered and the usertype is selected(which is a dropdown menu) some more fields show up depending on the usertype selection by the user. If selected Doctor the additional fields respective to the Doctor module show up, and same for the patient module.
To keep it simple on the front end this solution works like:
Loads Page with User Form
Submit User Form
Uses value to Load Next form
Submit Next Form + Redirect
Notes:
Uses the Values POSTed to determine what form is being submitted
Uses Initial to set User for the 2nd Form
This current flow could be broken up into 3 view with their own distinct URLs
Django View
def registerUser(request):
form = None
if request.method == 'POST':
valid = False
if 'usertype' in request.POST:
# 1st form submit
form = MyUserCreation(request.POST)
if form.is_valid:
valid = True
user = form.save(commit=False)
user.save()
login(request, user)
# Get 2nd form for load
if user.usertype == 'p':
page = 'patient'
form = PatientCreation(initial={'user':user})
elif user.usertype== 'd':
page = 'doctor'
form = DoctorCreation(initial={'user':user})
else:
# 2nd form submit
if 'dob' in request.POST:
form = PatientCreation(request.POST)
if form.is_valid:
form.save()
valid = True
elif 'deg' in request.POST:
form = DoctorCreation(request.POST)
if form.is_valid:
form.save()
valid = True
if valid:
# form sequence done
return redirect('home')
if not valid:
# a form failed somewhere
print(form.errors)
messages.error(request, 'Error occured')
if form == None:
page = 'general'
form = MyUserCreation()
context = {'form':form, 'page':page}
return render(request, 'rec/register_user.html', context )
Basic Django HTML Form
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
Now you could make this a single page by making the Template more complex, with JQuery Hiding/Showing extra fields based on dropdown Value on Change, but I assumed this would be the route you wanted.
Edit
To make the field disabled you'd just edit the forms.py
Note: the form-control is just showing that you can also add classes + extra attributes if you need to
class PatientForm(forms.ModelForm):
class Meta:
model = RunRequest
fields = (
'user',
'dob',
'contact',
)
def __init__(self, *args, **kwargs):
super(PatientForm, self).__init__(*args, **kwargs)
self.fields['user'].widget.attrs={'class': 'form-control', 'disabled':True}

form.is_valid always false

I've been trying to figure out why form.is_valid always return false but I still haven't figured it out yet and it doesn't help that I've just started with Django.
Model
class Post(models.Model):
STATUS_CHOICES = (
('d', 'Draft'),
('p', 'Published'),
)
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=120, null=True, blank=True)
text = models.TextField(max_length=500, null=True, blank=True)
slug = models.SlugField(max_length=40, unique=True)
publish_date = models.DateTimeField(null=True)
status = models.CharField(choices=STATUS_CHOICES, max_length=1,default='d')
Form
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title','author','slug','text','publish_date','status')
widgets = {
'title': forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Title'}),
'author': forms.Select(),
'slug': forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Slug'}),
'text': forms.Textarea(attrs={'class' : 'form-control', 'placeholder' : 'Text'}),
'publish_date': forms.DateInput(attrs={'class' : 'form-control', 'placeholder' : date.today}),
'status': forms.NullBooleanSelect(attrs={'class' : 'form-control'}),
}
View
def admin(request):
if request.user.is_authenticated:
if request.user.is_staff:
users = User.objects.all().order_by('date_joined').reverse()[:5]
posts = Post.objects.all().order_by('created').reverse()[:5]
publications = Publication.objects.all().order_by('created').reverse()[:5]
form = PostForm()
args = {'profile' : users ,'posts' : posts, 'publications' : publications, 'form' : form }
if request.POST:
print('request is posting')
if form.is_valid():
form.save()
else:
print('Error in saving')
return render(request, 'webadmin/index.html',args)
else:
return index(request)
else:
return redirect('/webmaster/login/')
I do hope that you can help me, Thank you OWO)/
Here:
form = PostForm()
if request.POST:
if form.is_valid():
you're trying to validate an unbound form, which is garanteed to fail - you have to pass data (in this case request.POST) to have something to validate. Your code should actually looks something like:
# explicitely test against request.method -
# you can have a POST request with an empty body,
# in which case `request.POST` will be empty and
# have a false value in a boolean test
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
# ...
Now even passing request.POST doesn't mean your form will necessarily validate, but just printing something like "'Error in saving'" will not tell you what's wrong or missing.
An invalid form has an .errors attributes that lists all the validation errors. Those validation errors are automatically displayed in your template if you use {{ form }} (or it's variants {{ form.as_p }}, {{ form.as_table }} etc to render the form in your template (which you did not post here). If you're rendering the form manually, you have to think of rendering the errors by yourself as well as documented here (I do hope you're at least using the form's fields for rendering instead of writing all the html form code manually...).
In all cases you can at least start with printing out form.errors in your view, so you have a clue about what doesn't validate, ie:
if form.is_valid():
form.save()
else:
print("errors : {}".format(form.errors))
Note that all of this is clearly documented, so you could have found the answers by yourself. I understand that Django is a rather large framework and that it can takes time to master it, but it has a rather good tutorial and extensive documentation with lot of examples, so you should really do the tutorial and start browsing the doc, at least to have an idea of what you can find in and where to search when something "doesn't work".

Django Forms Validation message not showing

I'm trying to restrict file type, size and extension that can be uploaded in a form. The functionality seems to work, but the validation error messages are not showing. I realize that if file._size > 4*1024*1024 is probably not the best way - but I'll deal with that later.
Here's the forms.py:
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ['name', 'description', 'url', 'product_type', 'price', 'image', 'image_url', 'product_file']
labels = {
'name': 'Product Name',
'url': 'Product URL',
'product_type': 'Product Type',
'description': 'Product Description',
'image': 'Product Image',
'image_url': 'Product Image URL',
'price': 'Product Price',
'product_file': 'Product Zip File',
}
widgets = {
'description': Textarea(attrs={'rows': 5}),
}
def clean(self):
file = self.cleaned_data.get('product_file')
if file:
if file._size > 4*1024*1024:
raise ValidationError("Zip file is too large ( > 4mb )")
if not file.content-type in ["zip"]:
raise ValidationError("Content-Type is not Zip")
if not os.path.splitext(file.name)[1] in [".zip"]:
raise ValidationError("Doesn't have proper extension")
return file
else:
raise ValidationError("Couldn't read uploaded file")
...and here's the view I'm using for that form:
def post_product(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = ProductForm(data = request.POST, files = request.FILES)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
product = form.save(commit = False)
product.user = request.user
product.likes = 0
product.save()
# redirect to a new URL:
return HttpResponseRedirect('/products')
What am I missing?
In your view, you are doing a redirect regardless of whether or not the form is valid - so there is nowhere for Django to show form errors.
The normal way to do this would be to re-render the form when is_valid() is False:
if form.is_valid():
# process the data in form.cleaned_data as required
product.save()
# redirect to a new URL - only if form is valid!
return HttpResponseRedirect('/products')
else:
ctx = {"form": form}
# You may need other context here - use your get view as a template
# The template should be the same one that you use to render the form
# in the first place.
return render(request, "form_template.html", ctx}
You may want to consider using a class-based FormView for this, as it handles the logic of re-rendering forms with errors. This is simpler and easier than writing two separate get and post views to manage your form. Even if you don't do that, it will be easier to have a single view that handles both GET and POST for the form.

How to add additional user information while the user is logged in?

I have created a login in page and now want to redirect the user to a page to add additional user information. How do I ensure that the user is "logged in" and that the information is added to that user's profiles? I know I need to use something like user=User.objects.get(email=email) to identify the user but I don't want to have the user enter their email on every single page to identify themselves. Is there something I can pass to keep the user Logged-in?
Here is the view:
def add_user_profile(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = AddUserProfileForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/summary/')
# if a GET (or any other method) we'll create a blank form
else:
form = AddUserProfileForm()
return render(request, 'registration/add_user_profile.html', {'form': form})
Here is the form:
class AddUserProfileForm(forms.ModelForm):
this_year = datetime.date.today().year
Years = range(this_year-70, this_year+1)
required_css_class = 'required'
first_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'First Name'}), label="")
middle_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Middle Name'}), label="")
last_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Last Name'}), label="")
date_of_birth = forms.DateField(widget=forms.extras.widgets.SelectDateWidget(years=Years))
Primary_address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Primary address'}), label="")
Primary_address_zipcode = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Zip Code'}), label="")
class Meta:
model = User
fields = ('first_name', 'middle_name', 'last_name', 'date_of_birth', 'Primary_address', 'Primary_address_zipcode')
Here is the url.py
url(r'^add_user_profile/$',
views.add_user_profile,
name='add_user_profile'),
You just need to create a view which will deal with request.user. If your update_profile functionality is rather simple you may take a look at UpdateView. You just need to overwrite get_object() to look like this:
def get_object(self, queryset=None):
return self.request.user
After this you just need to set up you UpdateView and create a proper template. You can read about this in the docs.

Django IntegrityError signup_simplesubscriber.date_created may not be NULL

I've read every "InterityError" + "may no be NULL" post and still can't track down what's causing this error.
I've got a two-part signup form. First part is just selecting a product. That passes a product ID to the next page as part of the URL, where they input personal info. I can get the form to work fine until I start removing fields -- i'm using model forms -- because some fields don't need to be displayed.
Here's my model, and the modelForm:
class SimpleSubscriber(models.Model):
name = models.CharField(max_length=255)
address = models.CharField(max_length=200)
city = models.CharField(max_length=100)
state = models.CharField(max_length=2)
zipcode = models.CharField(max_length=9)
phone = models.CharField(max_length=10)
email = models.EmailField()
date_created = models.DateTimeField(null=True)
sub_type = models.ForeignKey(Product)
def __unicode__(self):
return self.name
class SubscriberForm(ModelForm):
class Meta:
model = SimpleSubscriber
fields = ('name', 'address', 'city', 'state', 'zipcode', 'phone', 'email', 'sub_type',)#'date_created',
And here's my views:
def select_product(request):
title = "get yourself an e-edition. wurd."
pform = Product.objects.order_by('product_active')
if request.method == 'POST': # If the form has been submitted...
pform = ProductForm(request.POST) # A form bound to the POST data
if pform.is_valid(): # All validation rules pass
# ...
return HttpResponseRedirect('signup/%i' % pform.id) # Redirect after POST
else:
form = ProductForm() # An unbound form
return render_to_response('signup/index.html', {'title': title, 'pform': pform}, context_instance=RequestContext(request))
def subscriber_signup(request, product_id):
productchoice = Product.objects.get(id=product_id)
now = datetime.datetime.now()
title = "We need some information."
if request.method == 'POST': # If the form has been submitted...
sform = SubscriberForm(request.POST) # A form bound to the POST data
if sform.is_valid(): # All validation rules pass
sform.date_created = now
sform.sub_type = productchoice
sform.save()
return HttpResponseRedirect('thankyou/') # Redirect after POST
else:
sform = SubscriberForm() # An unbound form
return render_to_response('signup/detail.html', {'title': title, 'sform': sform, 'productchoice': productchoice, 'now': now.date(),}, context_instance=RequestContext(request))
I think it has something to do with the modelForm, but I'm pretty new, so I really have no idea. If I add all the fields to SubscriberForm, then they get filled out and everything works fine. But I don't want users to have to say when they filled out the form, so i put sform.date_created = now and I want the product_id to be filled in automatically by what choice they picked on the previous page. but if I exclude these fields from the form it throws the IntegrityError, which isn't very helpful in explaining what to change.
Any hints on where I'm messing up?
Thanks,
Two things:
1) You may benefit from using exlude in your form definition:
class SubscriberForm(ModelForm):
class Meta:
model = SimpleSubscriber
exclude = ('date_created', )
2) To your question, heres how to fix it:
if sform.is_valid(): # All validation rules pass
suscriber = sform.save(commit=False)
suscriber.date_created = now
suscriber.sub_type = productchoice
suscriber.save()
Alternatively to #fceruti's suggestion, you can also add more kwarg tags null=True on the model's field where appropriate - only forcing a minimal set of fields to be completed in the form.