I'm trying to implement a dropdown form which filters objects depending on the selection from the dropdown. I don't have any issues doing that but it gives me an error when nothing is selected and clicked submit. I want it to not filter anything and just give the entire list of the objects but i get the following error Specialization matching query does not exist on line
spec = Specialization.objects.get(name = s_name)
Here is the template where I've the form
<form action="/doclistings/" method="GET" >
<select class="form-control" id="selection" name="selection">
<option><b>Choose a Speciality...</b></option>
{% for value, text in form.selection.field.choices %}
<option name="choicemade" value="{{ value }}">{{ text }}</option>
{% endfor %}
<!-- {% csrf_token %} -->
</select>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Find Doctors</button>
</span>
</form>
here is the form
MY_CHOICES = (
('Dermatologist', 'Dermatologist'),
('Dentist', 'Dentist'),
('Orthopedist', 'Orthopedist'),
('Pediatrician', 'Pediatrician'),
)
class DropdownSelectionForm(forms.Form):
selection = forms.ChoiceField(choices=MY_CHOICES, widget = forms.Select, required = False)
genderselect = forms.ChoiceField(choices=GENDER_CHOICES, widget= forms.Select, required = False)
here is the view that's rendering the dropdown form
def index(request):
d = getVariables(request,dictionary={'page_name': "Home"})
if request.method == "POST":
form = DropdownSelectionForm(request.POST)
if form.is_valid():
selection = form.cleaned_data['selection']
return HttpResponseRedirect('/doclistings')
else:
form = DropdownSelectionForm()
return render(request, 'meddy1/index.html', {'form': form})
Here is the view that's rendering the objects based on the selection
def doclistings(request):
d = getVariables(request)
if request.method == "GET":
s_name = request.GET['selection']
if s_name == "":
doctors = Doctor.objects.all().order_by('-netlikes')
else:
spec = Specialization.objects.get(name = s_name)
doctors = Doctor.objects.filter(specialization = spec).order_by('-netlikes')
else:
return HttpResponseRedirect('/doclistings')
d['doctors'] = doctors
return render_to_response('meddy1/doclistings.html',d)
This is why you should use the QueryDict methods as this:
s_name = request.GET.get('selection', None)
if not s_name:
#if s_name is None
#...
That way it will fallback correctly if s_name is not present.
Related
The page returns the error as_crispy_field got passed an invalid or inexistent field after SUBMIT Button is clicked. I was trying to submit the form when the error is raised. The record is SUCCESSFULLY saved into the database but an error is raised. A form for the template was created to set up the fields. The Form was instantiated in the views so that it could easily map the elements from the FORM to the TEMPLATE.
What caused the error and how can I resolve it?
FORM: Here is my form code
class ModelCreateDescriptionForm(forms.ModelForm):
name = forms.CharField(max_length=100)
description = forms.Textarea()
model_type = forms.ChoiceField(
widget = forms.Select,
choices = MODEL_TYPE_CHOICES,
)
def __init__(self, project_id=1, *args, **kwargs):
super(ModelCreateDescriptionForm, self).__init__(*args, **kwargs)
# project = Project.objects.get(id=project_id)
self.fields['model_type'].choices = MODEL_TYPE_CHOICES
self.fields['model_type'].required = True
class Meta:
model = Model
fields = ['name', 'description', 'model_type']
VIEW: Here is my view
def modelCreationDescription(request, **kwargs):
pk = kwargs.get('pk')
project = Project.objects.get(id=pk)
context = {}
context['project'] = project
if request.method == 'POST':
form = ModelCreateDescriptionForm(pk, request.POST)
name = request.POST.get(u'name')
description = request.POST.get(u'description')
model_type = request.POST.get(u'model_type')
if not(name and model_type):
messages.warning(request, f'Please fill model name and model type!')
if form.is_valid():
formDescription = form.save(commit=False)
try:
formDescription.name = name
formDescription.description = description
formDescription.model_type = model_type
formDescription.project = project
except:
messages.warning(request, f'Something wrong!')
return redirect('all-model-listview')
# save the form value
formDescription.save()
messages.success(request, f'Model description successfully created')
return render(request, 'models/pred_steps/modelSetTargetFeatures.html', {'model_form': formDescription })
else:
form = ModelCreateDescriptionForm(project_id=pk)
context = {
'form': form,
'project': project,
'create_model_description': True,
}
return render(request, 'models/pred_steps/modelCreateDescriptions.html', context)
HTML: This is the template that returning an error
<div class="card border-left-info mb-1 shadow">
<div class="col-xl-10 col-md-8 mb-1">
{% if project %}
<p><h5 class="text-info">Project: {{ project }}</h5></p>
{% endif %}
<form method="POST">
{% csrf_token %}
<fieldset class='form-group'>
{{ form.name|as_crispy_field }}
</fieldset>
<fieldset class='form-group'>
{{ form.description|as_crispy_field }}
</fieldset>
<fieldset class='form-group'>
{{ form.model_type|as_crispy_field }}
</fieldset>
<div class="form-group">
{% if project.id %}
<a class="btn btn-outline-secondary float-right" href="{% url 'all-model-listview' %}">Cancel</a>
{% endif %}
<button class="btn btn-outline-success" type="submit">Submit and Continue</button>
</div>
</form>
</div>
</div>
In case the next template is causing the error here are the codes
FORM:
class ModelSetTargetFeaturesForm(forms.ModelForm):
target_column_classification = forms.ChoiceField(
widget = forms.Select,
)
target_column_regression = forms.ChoiceField(
widget = forms.Select,
)
def __init__(self, project_id=1, *args, **kwargs):
super(ModelSetTargetFeaturesForm, self).__init__(*args, **kwargs)
project = Project.objects.get(id=project_id)
df = pd.read_csv(project.base_file, encoding='ISO-8859-1')
cols = df.columns
a_cols = np.column_stack(([cols, cols]))
self.fields['target_column_classification'].choices = a_cols
self.fields['target_column_classification'].required=True
self.fields['target_column_regression'].choices = a_cols
self.fields['target_column_regression'].required=True
# project = Project.objects.get(id=project_id)
class Meta:
model = Model
fields = ['target_column_classification', 'target_column_regression', ]
VIEW:
def modelSetTargetFeatures(request, **kwargs):
pk = kwargs.get('pk')
model = Model.objects.get(id=pk)
project = Model.objects.get(project=model.project.pk)
context = {}
context['model'] = model
context['project'] = project
if request.method == 'POST':
form = ModelSetTargetFeaturesForm(pk,request.POST)
target_column_classification = request.POST.get(u'target_column_classification')
target_column_regression = request.POST.get(u'target_column_regression')
if not(target_column_regression and target_column_classification):
messages.warning(request, f'Please fill model name and model type!')
if form.is_valid():
formTargetFeatures = form.save(commit=False)
formTargetFeatures.target_column_classification = target_column_classification
formTargetFeatures.target_column_regression = target_column_regression
# save the form value
formTargetFeatures.save()
messages.success(request, f'Model description successfully created')
return render(request, 'models/pred_steps/modelFeaturesSelection.html', {'model_form': formTargetFeatures })
else:
form = ModelSetTargetFeaturesForm(model=pk)
context = {
'form': form,
'project': project,
'model': model,
'create_model_description': True,
}
return render(request, 'models/pred_steps/modelSetTargetFeatures.html', context)
TEMPLATE:
<div class="card border-left-info mb-1 shadow">
<div class="col-xl-10 col-md-8 mb-1">
{% if project %}
<p><h5 class="text-info">Project: {{ project }}</h5></p>
<p><h5 class="text-info">Model: {{ name }}</h5></p>
{% endif %}
<form method="POST">
{% csrf_token %}
<fieldset class='form-group'>
{{ form.target_column_classification|as_crispy_field }}
</fieldset>
<fieldset class='form-group'>
{{ form.target_column_regression|as_crispy_field }}
</fieldset>
<div class="form-group">
{% if project.id %}
<a class="btn btn-outline-secondary float-right" href="{% url 'all-model-listview' %}">Cancel</a>
{% endif %}
<button class="btn btn-outline-success" type="submit">Next: Feature Selection</button>
</div>
</form>
</div>
</div>
Your view is making use of two different html templates:
'models/pred_steps/modelCreateDescriptions.html'
and
'models/pred_steps/modelSetTargetFeatures.html'
The first one is used for presenting the form, upon a GET request, and allowing the participant to input their data: return render(request, 'models/pred_steps/modelCreateDescriptions.html', context)
However, once the participant's data are POST'ed, this happens:
form = ModelCreateDescriptionForm(pk, request.POST)
# ... inside of if_valid
formDescription = form.save(commit=False)
# ...
formDescription.save()
# ...
return render(request, 'models/pred_steps/modelSetTargetFeatures.html', {'form': formDescription })
It's this second rendition that is causing problems, so I assume you are using crispy fields in the modelSetTargetFeatures.html template also. When rendering this other template, you seem to try to include formDescription as a form. However, formDescription is not a form, because form.save(commit=False) returns a Model object, not a form. Hence, django/crispy forms doesn't understand what's going on and rightly says that what you are trying to use as a form is not a valid form (since it's actually a Model instance). If you really do want to pass the form itself on to the other template, you can simply use {'form': form}.
You might also want to use a better name for your model than simply Model. It's very confusing and might cause bugs, since the name is identical to that of django.db.models.Model, which we subclass from for creating specific Models. Additionally, you might want to use the pythonic naming convention of using snakecase (e. g. my_function_based_view) for functions, and camelcase with the first letter capitalized for classes (e. g. MyFormClass).
(note that the above response refers to the code as you originally posted it - you've done some edits since I started responding, but the basic issue seems to be the same even after the edits)
I have a form with MultipleChoiceField. I am able to save the data correctly. Now, if a user wants to edit that form he/she should see already selected items in the dropdown along with remaining all the other option. I want this as a function-based view. eg. I have 5 products in the dropdown and at the time of form submission, I selected products 1 and 2. Now, when I click on edit I should be able to see products 1 and 2 selected along with the other 3 products as unselected.Just help me with the edit view of this form. I am using same template for create and update. the code is messy.
models.py
class Lead(models.Model):
state = models.CharField(_("State"), max_length=255, blank=True, null=True)
type = models.CharField(max_length=20,choices=TYPE,blank=True,null=True)
products = models.ManyToManyField(Product,related_name='company_products',limit_choices_to=5,blank=True,null=True)
forms.py my customized form just sharing products section as code is getting bigger.
class LeadForm(forms.ModelForm):
product_queryset = []
products = forms.MultipleChoiceField(choices=product_queryset)
def __init__(self, *args, **kwargs):
assigned_users = kwargs.pop('assigned_to', [])
super(LeadForm, self).__init__(*args, **kwargs)
self.fields['products'].required = False
self.fields['products'].choices = [(pro.get('id'),pro.get('name')) for pro in Product.objects.all().values('id','name')]
views.py i am sharing just products section as the code is bit lenghty.
def update_lead(request, pk):
lead_record = Lead.objects.filter(pk=pk).first()
template_name = "create_lead.html"
users = []
if request.user.role == 'ADMIN' or request.user.is_superuser:
users = User.objects.filter(is_active=True).order_by('email')
else:
users = User.objects.filter(role='ADMIN').order_by('email')
status = request.GET.get('status', None)
initial = {}
if status and status == "converted":
error = "This field is required."
lead_record.status = "converted"
initial.update({
"status": status, "lead": lead_record.id})
error = ""
form=LeadForm(instance=lead_record,initial=initial,assigned_to=users)
if request.POST:
form = LeadForm(request.POST, request.FILES,
instance=lead_record,
initial=initial, assigned_to=users)
if form.is_valid():
if request.POST.getlist('products', []):
lead_obj.products.clear()
lead_obj.products.add(*request.POST.getlist('products'))
else:
lead_obj.products.clear()
status = request.GET.get('status', None)
success_url = reverse('leads:list')
if status:
success_url = reverse('accounts:list')
return JsonResponse({'error': False, 'success_url': success_url})
return JsonResponse({'error': True, 'errors': form.errors})
context = {}
context["lead_obj"] = lead_record
context["lead_form"] = form
context["teams"] = Teams.objects.all()
context['products'] = Product.objects.all()
context["assignedto_list"] = [
int(i) for i in request.POST.getlist('assigned_to', []) if i]
return render(request, template_name, context)
create_lead.html i am using this html for create as well as update view. I am just sharing products div section
<div class="form-group" style="height:20px;">
<label for="exampleInputEmail1">Product{% if products.field %}<span
class="error">*</span>{% endif %}</label>
<select multiple="multiple">
{% for product in products %}
<option value="{{product.pk}}" {% if product in products.company_products.all %} selected="" {% endif %}>{{product.name}}</option>
{% endfor %}
</select>
</div>
On template, you should to avoid render form "by hand". You should to render form using django forms rendering system.:
<form action="/your-name/" method="post">
{% csrf_token %}
{{ lead_form }}
<input type="submit" value="Submit">
</form>
If you need to render just this field manually:
<div class="form-group" style="height:20px;">
<label for="exampleInputEmail1">Product{% if products.field %}<span
class="error">*</span>{% endif %}</label>
{{ lead_form.products }}
</div>
I've a form
class ScoreUpdateForm(forms.Form):
answer_id = forms.IntegerField()
given_score = forms.FloatField()
CHOICES = [('calculated_score', 'Calculated Score'),
('given_score', 'Given Score')]
final_score = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())
Here is my template
<form action="/teachers/questions/{{ answer.question.id }}/score" method="post">
{{ form.given_score }}
{{ form.final_score }}
<input type="submit" value="Submit"/>
</form>
here is my view
def score(request, pk):
if request.method == 'POST':
form = ScoreUpdateForm(request.POST)
if form.is_valid():
given_score = form.cleaned_data['given_score']
final_score = form.cleaned_data['final_score']
I can get given_score value but not the final_score. I want to get the value of the selected choice in my view after the post.
try to use
final_score = form.cleaned_data['final_score']
final_score = dict(form.fields['final_score'].choices)[final_score]
I'm trying to implement a form where I can get userinput, but for some reason the form in not showing in the template. I have two fields in the form and one of the field is a dropdown menu. The template is not showing the dropdown list.
Here is the form that I'm trying to use
TIME_CHOICES = (
(5, 'Less than 5 Minutes'),
(10, 'Less than 10 Minutes'),
(15, 'Less than 15 Minutes'),
)
class UserContentForm(forms.ModelForm):
time = forms.ChoiceField(required=True, choices = TIME_CHOICES, widget = forms.Select)
comment = forms.CharField(max_length=2000, required= False,widget=forms.TextInput())
class Meta:
model = UserContent
fields = ("time","comment")
Here is the view where I'm tyring to save the form
def addContent(request, id):
d = getVariables(request)
profile = Doctor.objects.get(id=id)
if request.user.is_authenticated():
user = request.user
ds = DoctorSeeker.objects.get(user=user)
d['doctorseeker'] = ds
doctorLiked = Like.objects.filter(doctor_id=profile.id,user_id=user.id)
d['my_doctor'] = profile.id == request.user.id
d['username'] = user.username
if doctorLiked:
d['liked'] = True
else:
d['liked'] = False
if request.method == "POST":
form = UserContentForm(request.POST)
if form.is_valid():
time = form.cleaned_data['time']
comment = form.cleaned_data['comment']
con = UserContent(time=time, comment = comment, doctor_id = profile.id, user_id = request.user.id)
con.save()
return render(request,'meddy1/docprofile.html',{'doctor': profile})
else:
form = UserContentForm()
d.update({'doctor': profile, 'UGC': UserContent.objects.all()})
return render(request, 'meddy1/usercontent.html',d)
here is the template where I'm trying to render it
<form action="" method="post" id="user_uploader" > {% csrf_token %}
<input type="hidden" name="user" value="{{ user.id }}" />
<input type="hidden" name="doctor" value="{{ doctor.id }}" />
<select class="form-control" id="s1" name="time">
<option><b>Select a Time...</b></option>
{% for value, text in form.time.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<input type="text" class="form-control" id="comment" placeholder="Comment" name="comment">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Submit Review</button>
</form>
Here is the model
class UserContent(models.Model):
time = models.IntegerField(blank = True)
comment = models.TextField(blank = True)
doctor = models.ForeignKey(Doctor)
user = models.ForeignKey(User)
submitted_on = models.DateTimeField(auto_now_add=True)
You are not passing the form variable to template. Update the line to
d.update({'doctor': profile, 'UGC': UserContent.objects.all(),
'form': form #add form variable
})
Also, instead of manually rendering select tag you can do {{ form.time }} to render it.
I am working on an e-commerce app and want customers to be able to order several items. Currently they can only order one and i am having a hard time figuring out how i can achieve this.
in my menu template i return all the items in their category as follows:
{% for category in categories %}
<h5> {{ category.title }} </h5>
{% for item in category.item.all %}
<li><a href="{% url item_order item.id %}">{{item.name}}<span> {{item.price}}frw</span></li><a/>
OnClick, each item redirects to a form where you can order the quantity. here is the view:
def show_item(request,id):
# need to evaluate the HTTP method
if request.method == 'POST':
a = Item.objects.get(pk=id)
form = forms.PartialOrderItemForm(request.POST,instance=a)
# check validation of posted data
if form.is_valid():
order.add_to_order(request)
# if test cookie worked, get rid of it
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
url =urlresolvers.reverse('order_index')
# redirect to order page
return HttpResponseRedirect(url)
else:
# it's a GET, create the unbound from. Note request as a Kwarg
form = forms.PartialOrderItemForm(request.GET)
# set the test cookie on our first GET request
request.session.set_test_cookie()
context={
# 'categories':categories,
'form':form,
# 'menu':menu,
}
return render_to_response('item.html',context,context_instance=RequestContext(request))
At some point, the above view calls the following view, that is after validating the form order.add_to_order(request)
add an item to order
def add_to_order(request):
postdata = request.POST.copy()
# get item slug from post data, return blank if empty
# item_slug = postdata.get('item_slug','')
#get quantity added, return 0 if empty
quantity = postdata.get('quantity',0)
# fetch the item or return missing page error_message
i = get_object_or_404(Item,pk=1)
i.orderitem_set.all()
# get items in order
order_items = get_order_items(request)
item_in_orders = False
# check to see if item is already in order
for order_item in order_items:
if order_item.item.id == i.id:
#update the quantity if found
order_item.augment_quantity(quantity)
item_in_orders = True
if not item_in_orders:
# creat and save a new order item
oi = OrderItem()
oi.order_id = _order_id(request)
oi.quantity = quantity
oi.item = i
oi.save()
Once the form is validated, the order is shown in the order page. here is the view that does this
def show_order(request):
if request.method == 'POST':
postdata = request.POST.copy()
if postdata['submit'] == 'Remove':
order.remove_from_order(request)
if postdata['submit'] == 'Update':
order.update_order(request)
order_items = order.get_order_items(request)
# page_title = 'F4L order'
order_subtotal = order.order_subtotal(request)
context = {
'order_items':order_items,
'order_subtotal':order_subtotal,
}
return render_to_response('public/order.html',context,context_instance=RequestContext(request))
here is part of the template for the order page.
<tbody>
{% if order_items %}
{% for item in order_items %}
<tr>
<td>
{{ item.item.name }}
</td>
<td>{{ item.item.price }}<span> frw</span></td>
<td class="right">
<form method="post" action="." class="order">
{% csrf_token %}
<label for="quantity">Quantity:</label>
<input type="text" name="quantity" value="{{ item.quantity }}" id="quantity" size="2" class="quantity" max_length="5" />
<input type="hidden" name="item_id" value="{{ item.id }}" />
</td>
<td>
<input type="submit" name="submit" value="Update" />
</form>
Here is what is happening, when i order the first item with pk=1 it works out properly, but then when i order the second item or any other and fill in the form, instead the order page returns the first item always.
How do you think i should go about this.