I am trying to do a very simple django webpage where a user fills 2 fields of a form with two numbers, submits them and gets a result at the third field. I am using ModelForms.
For the moment I am only interested in the first part, where the user navigates to the url and is prompted a page with the empty form. I do the following, but the page I see at my browser has no form, and only the submit button. I don't know if that's because the ModelForm I try to embed in the html code is still empty, but I tried to give it default values and it still didn't work.
Any help will be very appreciated! :)
urls.py:
urlpatterns = patterns('',
url(r'^calculator/', views.calculate)
)
models.py:
class Calc(models.Model):
field1 = forms.CharField()
field2 = forms.CharField()
field3 = forms.CharField()
class CalcForm(ModelForm):
class Meta:
model = Calc
views.py:
def calculate(request):
c = CalcForm()
if request.method == 'POST':
#do the math
#return ...
else:
return render_to_response('home.html', {'calculator': c})
home.html:
<form action="/calculator/" method="post">
<table>
{{ calculator }}<!--I also tried .as_p, .as_ul...-->
</table>
<input type="submit" value="Submit" />
</form>
change the following:
class Calc(models.Model):
field1 = forms.CharField()
field2 = forms.CharField()
field3 = forms.CharField()
to
class Calc(models.Model):
field1 = models.CharField(max_length=10)
field2 = models.CharField(max_length=10)
field3 = models.CharField(max_length=10)
Did you ever syncdb with the above code? Because if you would've, it must have thrown an error. This makes me think if your db has the correct tables or not. Make sure you syncdb after the above change.
Also, you should always check if an instance exist, This will also help you troubleshoot that if the object was correctly passed or not:
{% if calculator %}
<h3>Calculation Form</h3>
<form action="/calculator/" method="post">
<table>
{{ calculator }}
</table>
<input type="submit" value="Submit" />
</form>
{% endif %}
Your model should use models.CharField, not forms.CharField.
Related
So im trying to make this page where i add new category which only has name. But when i try it, i just get a "non-string type" as the name. So it works i guess just doesn't take whatever i give it in my input in html.
HTML:
<input type="text" class="form-control" placeholder="Name" id = "category-create-name" name=
"category_name">
<input type="submit" class="login-button" value="Post">
Model:
class Category(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=255, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Form:
class CategoryCreateForm(forms.ModelForm):
class Meta:
model = Category
fields = ('name',)
widgets = {
'category_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Name', 'id':'category-create-name'}),
}
View:
class CategoryCreateView(CreateView):
form_class = CategoryCreateForm
template_name = 'product/new_category.html'
success_url = '/dashboard/'
There are a couple of things I see here. First in your form. In the widgets attribute of class Meta, the keys in that dictionary need to be a field name that is in your fields attribute. In other words you need to change 'category_name' to 'name'. Second, in your template that is used for the view. You seem to be manually defining a separate input field rather than the one that your view and form are expecting. You should just define the form in your template like this:
<form method="POST">
{% csrf_token %}
{{ form_as_p }}
<input type="submit" class="login-button" value="Post">
</form>
In the template {{ form.as_p }} will take the form that you gave to your view, and automatically create it in the html when it is being rendered to the page.
I want to update user info with UserChangeForm and things go pretty well except for the ManyToManyField. When I render the page I can see that all user informations are displayed in correct order of each field like user's username will be in the username field but it's blank in manytomanyfield.
#model.py
class Department(models.Model):
name = models.CharField(max_length=100)
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('Email Address'), unique=True)
department = models.ManyToManyField(Department)
# some other fields
# forms.py
class EditUserForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ['email', 'department', ..]
widgets = {'department': forms.CheckboxSelectMultiple()}
# view.py
def home(request):
template_name = "app/home.html"
edit_form = EditUserForm(instance=request.user)
if request.method == "POST":
edit_form = EditUserForm(request.POST, instance=request.user)
if edit_form.is_valid():
edit_form.save()
return JsonResponse({'success': True}, status=200)
else:
return JsonResponse({'error': edit_form.errors}, status=400)
return render(request, template_name, {'edit_form': edit_form})
# template
<form action="{% url 'home' %}" method="POST">
<div class="row">
{{edit_form.email}}
{{edit_form.first_name}}
{% for department in edit_form.department %}
<h6 id="checkbox">{{department.tag}} {{department.choice_label}}</h6>
{% endfor %}
</div>
</form>
here is the picture
As you can see the names and email are displaying inside the form field but why all checkboxes are empty? (Checkbox fields are department)
If you just want to render the field you don't need a for loop. You can just use {{edit_form.department}}. In case you need to modify each input field in CheckboxSelectMultiple you should loop through edit_form.department.field.choices.
For example:
{% for choice, value in edit_form.department.field.choices %}
<input type="checkbox" name="{{choice.instance.value}}" value="{{choice.instance.pk}}" id="id_{{choice.instance.value}}">
{% endfor %}
Note that this will work only in django 3.0 and newer.
you need pass the value and the name in the input in your template
{% for value, name in edit_form.fields.department.choices %}
<input type="checkbox" name="{{name}}" value="{{value}}" id="{{name}}">
{% endfor %}
hello everyone i am trying to save a form in my database
my models.py
class customer(models.Model):
customerid = models.CharField(default=str(uuid.uuid1()), max_length=500, primary_key=True)
customername=models.CharField(max_length=1000,)
my forms.py
from .models import customer
class createcustomerform(ModelForm):
class Meta:
model=customer
fields=['customername']
my views.py
def createcustomer(request):
if request.method=='GET':
return render (request,'marketing/createcustomer.html',{'form':createcustomerform()})
else:
try:
form=createcustomerform(request.POST)
newcreatecustomer=form.save(commit=False)
newcreatecustomer.user=request.user
newcreatecustomer.save()
return redirect('createcustomer')
except ValueError:
return render(request,'marketing/createcustomer.html',{'form':createcustomerform(),'error':'Check for Invalid Data. Try Again.'})
my html is
<form method="POST">
{% csrf_token %}
<div id="customernamename"> <span>Company Name</span>
<input type="text" id="customername" placeholder="Enter Company's Full name"> </div>
</form>
When i am clicking save button its redirecting me again to createcustomer but not saving data in my database. kindly help what is wrong or what am i missing ?
Its saving the customer by admin area not by html page.
Your mistake is here: newcreatecustomer.user=request.user
The variable newcreatecustomer does not have user field; that is why it is not working.
I'm trying to get a simple form working. Oddly, other forms I wrote in this app are working fine, but this one wont show the fields. Can anyone tell me what I'm missing? Here are the files
views.py:
def newnote(request, record_id):
if request.method == 'POST':
form = NoteForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/tracker/all/')
else:
form = NoteForm()
return render(request, 'tracker/noteform.html', {'form': form})
models.py
class Note(models.Model):
record = models.ForeignKey(Record, on_delete=models.CASCADE)
note_text = models.CharField('Notes', max_length=2000)
note_date = models.DateField('Date Entered')
forms.py
class NoteForm(forms.Form):
class Meta:
model = Note
fields = ['note_text',
'note_date'
]
template (noteform.html)
<form action="/tracker/newnote/" method="post">
<div id="fields">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</div>
</form>
One other note, I have commented out the div id called "fields", to rule out CSS as the issue.
Your form is based on form.Form, which doesn't know anything about models, doesn't expect a Meta class, and expects all its fields to be declared manually - since you have not declared any fields, nothing will show on the template.
It should inherit forms.ModelForm instead.
I used this code previously it worked fine and i was suggested to use ModelForm by another member, it did make sense to use the form.is_valid() function etc.. so thought of giving it a try.
I went through some other examples on the internet but mine does not seem to work for some reason, or may be I am not doing it right, I get the following when I print the form in the view, and it goes to the else statement, so my form does not get saved
<input id="id_product" type="text" name="product" value="aassddf" maxlength="250" />
FAIL
My model.py
from django.db import models
from django.forms import ModelForm
class Category(models.Model):
name = models.CharField(max_length=250)
def __unicode__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category)
product = models.CharField(max_length=250)
quantity = models.IntegerField(default=0)
price = models.FloatField(default=0.0)
def __unicode__(self):
return self.product
class ProductForm(ModelForm):
class Meta:
model = Product
My views.py
from models import *
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
def index(request):
...
...
def add_product(request):
if request.method == 'POST':
form = ProductForm(request.POST)
print form['product']
if form.is_valid():
form.save()
return HttpResponseRedirect('/product')
else:
print 'FAIL'
return HttpResponseRedirect('/product')
My html
<form method="post" action="add_product/">
{% csrf_token %}
<label for="category">Category</label>
<select name="category" id="category">
{% for category in category_list %}
<option> {{ category.name }} </option>
{% endfor %}
</select>
<label for="product">Product</label>
<input type="text" name="product" id="product">
<label for="quantity">Quantitiy</label>
<input type="text" name="quantity" id="quantity">
<label for="price">Price</label>
<input type="text" name="price" id="price">
<input type="submit" value="Add New product" id="create">
</form>
Is there a better way i could save the data, using ModelForms ??
Thanks in advance for the help.
You should read the documentation. If the form is not valid, it will have a whole set of errors associated with it, which will tell you exactly why. But you just throw that away, and redirect to /product. The docs show exactly how to redisplay the form with the errors.
Also you should not write HTML form field tags directly in your template: use the form object from the view - {{ form.product }}, etc - as these will be repopulated with the appropriate values on redisplay.
Thanks to Daniel Roseman and Anuj Gupta I think I finally re-worked on my code on got it working in a standard way so it will generate the html form and validate errors.
So for anyone else who is trying to work django forms here is the code I worked on.
My model.py is was almost the same one i posted on the question but i removed
class ProductForm(ModelForm):
class Meta:
model = Product
I created a new form.py here is the code-
from django import forms
from models import Category
class ProductForm(forms.Form):
# Put all my Categories into a select option
category = forms.ModelChoiceField(queryset=Category.objects.all())
product = forms.CharField()
quantity = forms.IntegerField()
price = forms.FloatField()
My views.py changed had a lot of changes -
def add_product(request):
success = False
if request.method == "POST":
product_form = ProductForm(request.POST)
if product_form.is_valid():
success = True
category = Category.objects.get(name=product_form.cleaned_data['category'])
product = product_form.cleaned_data['product']
quantity = product_form.cleaned_data['quantity']
price = product_form.cleaned_data['price']
new_product = Product(category = category, product = product, quantity = quantity, price = price )
new_product.save()
new_product_form = ProductForm()
ctx2 = {'success':success, 'product_form':new_product_form}
return render_to_response('product/add_product.html', ctx2 , context_instance=RequestContext(request))
else:
product_form = ProductForm()
ctx = {'product_form':product_form}
return render_to_response('product/add_product.html', ctx , context_instance=RequestContext(request))
Finally in my html page i used {{ product_form.as_p }} so it created the forms dynamically
{% if success %}
<h3> product added successfully </h3>
{% endif %}
<form method="post" action=".">
{% csrf_token %}
{{ product_form.as_p }}
<input type="submit" value="Add New product" id="create">
<input type="reset" value="reset" id="reset">
</form>
This may not be the perfect solution, but for a starter like me this sounds good, and at times you just get lost while reading the docs lol, hope it helps some one.
Cheers
Try:
<form method="post" action="add_product/">
{% csrf_token %}
{{ form.as_p }}
</form>
in your template, instead of hand-coding the form's input tags. This shortcut will generate the form html for you, as well as print validation errors.
Make sure you return the form object to the template when:
There is no request.POST (form has not been submitted)
form.is_valid() fails (form has validation errors)
Of course, this is only to get you started. You really should read the docs