Django HTML Dropdown - python

I am trying to make a html dropdown and pass the values into Postgrase SQL database. My dropdown values are being retrieved from another database table. It gives me a MultiValueKeyDictError every time I submit the form. I know I can use forms.py to do the same thing but I want to explore the HTML way of doing this.
My HTML file
<form action = "" method = "post">
{% csrf_token %}
<label for = "LogType"></label>
<input id ="LogType" type = "text" value = "{{ user.department }}">
<label for ="DelayCategory">Delay Category</label>
<select id = "delaycategory" class = "form-control">
{%if user.department == 'TechAssembly'%}
{%for techdelay in techdelay%}
<option value = "{{ techdelay.DelayCode }}">{{ techdelay.DelayCategory}}</option>
{%endfor%}
{%endif%}
{%if user.department == 'Testing'%}
{%for testdelay in testdelay%}
<option value = "{{ testdelay.DelayCode }}">{{ testdelay.DelayCategory}}</option>
{%endfor%}
{%endif%}
</select>
<label for = "iterations">Iterations</label>
<input type = "number" id = "iterations">
<center><input type="submit" value=Submit id = "button"></center>
</form>
My Views.py file
def rulesView(request, user_name):
testdelay = TestingDelayCategory.objects.all()
techdelay = TechDelayCategory.objects.all()
if request.method == "POST":
rulesnew = rules()
rulesnew.DelayCategory = request.GET['DelayCategory']
rulesnew.LogType = request.POST('LogType')
rulesnew.iterations = request.POST('iterations')
rulesnew.save()
context = {
'techdelay':techdelay,
'testdelay':testdelay,
}
return render(request, 'rules/rules.html', context)

rulesnew.DelayCategory = request.GET['DelayCategory']
rulesnew.LogType = request.POST('LogType')
rulesnew.iterations = request.POST('iterations')
Have a second look at this: request.GET should be request.POST and request.POST('LogType') should be request.POST['LogType'] same with iterations.
The error message should include the exact line where the errors was raised. So it would have been way easier to debug if you have told us that the error was raised e.g. in this line rulesnew.LogType = request.POST('LogType')

Related

Flask for to have result as variable

I have below form which selects the data and redirects to the page user_data
It selects the date and redirects to another page.
Am able to get the data using request.form['Period'] method in python.
But this is not getting called in form action <form action = "/user_data/{{period}}" method="POST"> period variable is empty resulting in Not Found error.
Is there a way to select a value and pass it into a same form
<form action = "/user_data/{{period}}" method="POST">
<label for = "Period" id = "Period">Period</label>
<input list="User_Listing_Period" id ="User_Lst_Period" name = "Period">
<datalist id = "User_data_Period">
{% for row in user_dat_filter %}
<option name = "User_Dat_Period" value = "{{row[0]}}"></option>
{% endfor %}
</datalist>
<div class="submit">
<label for = "Submit" id = "Submit"></label>
<button type="submit">Submit</button>
</div>
two options here:
let form direct to url /user_data, and based on the Period value renders the page i.e it renders the data for that month.
as value is based on user selection, JS can be utilized.
<html>
<body>
<form action = "/user_data/{{period}}" method="POST" id="myForm">
<label for = "Period" id = "Period">Period</label>
<input list="User_Listing_Period" id ="User_Lst_Period" name = "Period">
<datalist id = "User_Listing_Period">
<!-- I commented this, so can check in HTML, you can go with your code
{% for row in user_dat_filter %} -->
<option name = "User_Dat_Period" value = "Aug22"></option>
<option name = "User_Dat_Period" value = "Sep22"></option>
<option name = "User_Dat_Period" value = "Oct222"></option>
<!-- {% endfor %} -->
</datalist>
<div class="submit">
<label for = "Submit" id = "Submit"></label>
<button type="submit">Submit</button>
</div>
</form>
<script>
const myform= document.getElementById("myForm")//identify the form
// add a listener to myform
myform.addEventListener('submit', async (e) => {
e.preventDefault(); //block default submit
const selectedOption=document.getElementById("User_Lst_Period")
let value = selectedOption.value
document.location.href='/user_data/' + value //redirect to value selected by user
})
</script>
</body>
</html>

Taking value of selected radio buttons from one page in Django

I want to take value of selected radio buttons in a single page. To explain this here is my code in models.py file
class Quiz(models.Model):
Description = models.CharField(max_length=200)
def __str__(self):
return str(self.Description)
class Question(models.Model):
Question_Text = models.CharField(max_length=500)
Option1 = models.CharField(max_length=100)
Option2 = models.CharField(max_length=100)
Option3 = models.CharField(max_length=100, blank=True)
Option4 = models.CharField(max_length=100, blank=True)
Answer = models.CharField(max_length=100)
QuizID = models.ForeignKey(Quiz, on_delete=models.CASCADE)
def __str__(self):
return str(self.Question_Text)
I want user to choose one option from "Option1, Option2, Option3, Option4" fields and I want to get value of the selected radio button.
Here's my try:
disp.html
<form action="" method="post">
{% csrf_token %}
{% for q in question %}
{{ q.Question_Text }} <br>
<input type="radio" id="question_{{ q.Option1 }}" name="{{ q.id }}" value="{{ q.Option1 }}">
<label for="question_{{ q.Option1 }}">{{ q.Option1 }}</label> <br>
<input type="radio" id="question_{{ q.Option2 }}" name="{{ q.id }}" value="{{ q.Option2 }}">
<label for="question_{{ q.Option2 }}">{{ q.Option2 }}</label> <br>
{% if q.Option3 %}
<input type="radio" id="question_{{ q.Option3 }}" name="{{ q.id }}" value="{{ q.Option3 }}">
<label for="question_{{ q.Option3 }}">{{ q.Option3 }}</label> <br>
{% endif %}
{% if q.Option4 %}
<input type="radio" id="question_{{ q.Option4 }}" name="{{ q.id }}" value="{{ q.Option4 }}">
<label for="question_{{ q.Option4 }}">{{ q.Option4 }}</label> <br>
{% endif %}
{% endfor %}
<br> <input type="submit" class="btn btn-primary" value="Submit">
</form>
Now I want to get value of the selected radio button. Since the number of records may vary and hence I cannot take value of the selected radio buttons manually like:
first = request.POST.get("11")
second = request.POST.get("18")
third = request.POST.get("19")
Here 11,18 and 19 are the value of id field of records in Question Model.
So I tried for loop to do this in the following way but I am getting "None" as value.
view.py
def disp(request):
quiz = get_object_or_404(Quiz, id = 4)
question = get_list_or_404(Question, QuizID = quiz)
if request.method == "POST":
for q in question:
response = request.POST.get("q.id")
print(response)
return render(request, 'quiz/disp.html', {'question' : question, 'quiz' : quiz})
Please help me in getting the value of the selected radio button or if there's another way to get the value of selected radio button, please suggest me
POST <QueryDict> contains only keys of type <str>. Which means that request.POST.get(18) will return None and request.POST.get('18') will return the value (assuming that the key exists in POST <QueryDict>).
if request.method == "POST":
for q in question:
# convert the q.id into str
response = request.POST.get(str(q.id))
print(response)
OR
you may loop through the POST data and check for valid keys. Since, POST data contains other data like csrfmiddlewaretoken.
Change the value of radio button name attribute into the form of question_{{ q.id }}.
<input type="radio" id="question_{{ q.Option1 }}" name="question_{{ q.id }}" value="{{ q.Option1 }}">
Loop through POST and check if the key contains question_
if request.method == "POST":
for k, v in request.POST.items():
if 'question_' in k:
# do something
In view.py, you are using "q.id" as a HTTP param name while I think you intended it to be 11, 18, 19 etc. Try this :-
if request.method == "POST":
for q in question:
response = request.POST.get(q.id)
print(response)
I would reconfigure the Question model to use Choices:
SOME = 'win'
OTHER = 'lin'
Answer_CHOICES = (
(SOME, 'Some Answer'),
(OTHER, 'Other Answer'),
)
class Question(models.Model):
....
Options = models.CharField(choices=Answer_CHOICES, max_length=100)
....
Check the Choices field and how you may use it. Also, check crispy forms which automatically handle the form input for you.

How can I set values for each HiddenInput fields of Django?

I wrote codes, but I don't know how to set 'name' and 'value' of hidden tag with Django template. I read Django's Widgets Docs, but I couldn't find the way.
(Pdb) print(errors)
<ul class="errorlist"><li>friend_id<ul class="errorlist"><li>This field is required.</li></ul></li><li>add_remove<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
First, I tried to write like
<input type="hidden" name="friend_id" value="{{ user_info.user_id }}">
and
friend_id = request.POST.friend_id
But I couldn't get how to get POST values without Django's Form. So, I used Django's Form with following codes.
views.py
from myapp.forms import HiddenUserPage
hiddenform = HiddenUserPage
if request.method == 'POST':
hidden = hiddenform(request.POST)
if hidden.is_valid():
from myapp.models import Friends
try:
friend_id = hidden.cleaned_data['friend_id']
add_remove = hidden.cleaned_data['add_remove']
if add_remove == "add":
f = Friends(user_id=request.user.user_id, friend_id=friend_id)
f.save()
elif add_remove == "remove":
f = Friends.objects.filter(user_id=request.user.user_id).get(friend_id=friend_id)
f.delete()
except:
errors = "DB error"
else:
errors = hidden.errors
else:
hidden = hiddenform()
errors = ""
view = {
'errors': errors,
'hidden': hidden,
}
template = 'myapp/user/user_page.html'
return render(request, template, view)
forms.py
class HiddenUserPage(forms.Form):
friend_id = forms.CharField(widget=forms.HiddenInput())
add_remove = forms.CharField(widget=forms.HiddenInput())
user_page.html
<form method="POST" action="" class="">
{% csrf_token %}
<p class="submit">
<button class="confirmbutton" type="submit">
{% if is_friend %}
remove friend
<!-- # I'd like to write like # -->
<!-- <input type="hidden" name="friend_id" value="remove"> # -->
<!-- <input type="hidden" name="friend_id" value="{{ user_info.user_id }}"> # -->
{{ hidden.add_remove }}
{{ hidden.friend_id }}
{% else %}
add friend
<!-- <input type="hidden" name="friend_id" value="add"> # -->
<!-- <input type="hidden" name="friend_id" value="{{ user_info.user_id }}"> # -->
{{ hidden.add_remove }}
{{ hidden.friend_id }}
{% endif %}
</button>
</p>
</form>
Sorry, my code is filthy.
Looks like the question is in providing initial data to the form, then it's is generally done in the view passing initial to the form instantiation, e.g.:
# In your view.py
def ...(...):
# Inside your view function
if request.method == 'GET':
# Provide initial data to the form here
# Get your 'user_info' from models or sessions,
# or wherever you keep it
hidden = hiddenform(initial={"friend_id":user_info.user_id})
if reuest.method == 'POST':
hidden = hiddenform(request.POST)
# Process posted form data
...
# More code general for both HTTP verbs
view = {'errors': errors, 'hidden': hidden}
template = 'myapp/user/user_page.html'
return render(request, template, view)
You might also want to bound the form to model data directly, see the docs for more info.

Python. Django1.7 DoesNotExist. Matching query does not exist

I'm a learner in Python and Django.
I am trying to pass selected checkboxes to my views and then make a get() call to fetch related objects, but keep getting the error DoesNotExist, even though the object is present in the database.
I tried changing the get() parameters but it still shows the same error, as if it just cannot fetch the database. Please help!
ERROR IS IN THE #ed LINE
Here is my views.py
def manage(request):
if request.POST.get('taskname'):
name = request.POST.get('taskname')
end = request.POST.get('enddate')
emp = User.objects.get(username = request.user.username)
print emp.username
try:
newtask = Task(taskname = name, deadline = end, status = 'incomplete', empid = emp)
newtask.save()
except:
print "not saved"
my_tasks = Task.objects.filter(empid = emp)
return render(request, 'ellipse/dashboard.html', {'employee': emp, 'tasks': my_tasks})
else:
selected = request.POST.get('dropdown')
if selected == 'Delete':
tasks = request.POST.getlist('t')
emp = User.objects.get(username = request.user.username)
for seltask in tasks:
#deltask = Task.objects.get(taskname=seltask)
deltask.delete()
my_tasks = Task.objects.filter(empid = emp)
return render(request, 'ellipse/dashboard.html', {'employee': emp, 'tasks': my_tasks})
And, my html:
<div>
<form action="/ellipse/manage/" method="post">
{% csrf_token %}
<p>Taskname <input type="text" name="taskname"></p>
<p>Deadline <input type="date" name="enddate"></p>
<select name="dropdown">
<option selected="selected" disabled>Select action :</option>
<option value="Add">Add</option>
<option value="Delete">Delete</option>
<option value="Mark as complete">Mark as complete</option>
<option value="Mark as incomplete">Mark as incomplete</option>
</select>
{% if tasks %}
{% for tasko in tasks %}
<p><tr><td><input type="checkbox" name="t" value={{ tasko.taskname }}/></td><td>{{ tasko.taskname }}</td><td>{{ tasko.deadline }}</td><td>{{ tasko.status }}</td></tr></p>
{% endfor %}
{% endif %}
<p><button type="submit" name="modify">Modify</button></p>
</form>
</div>
I am clueless on how to proceed further and it'd be great help if this issue can be resolved. Thanks in advance!
Well your get looks jacked up.
emp = User.objects.get(username = request.user.username)
should probably be something like this.
emp = User.objects.get(id=request.user.id)
You could probably do this to...
emp = User.objects.get(user=request.user)

Django 1.6: Two dropdown menus in one single form not working

I've two dropdown menus in one single template. They both are using the same form. But for some reason only one of the form works and gives me MultiValueDictKeyErrorin the views. It gives me error on the line request.session["genderselect"] = request.POST['genderselect']
So I've commented out the lines to see what happens and the code works and shows the first dropdown (name = selection). But the second dropdown of (name = genderselect) doesn't work, although they both are part of the same form.
views.py
def doclistings(request):
d = getVariables(request)
if request.method == "POST":
form = DropdownSelectionForm(request.POST)
if form.is_valid():
print form.errors
selection = form.cleaned_data['selection']
# genderselect = form.cleaned_data['genderselect']
# request.session["genderselect"] = request.POST['genderselect']
request.session["selection"] = request.POST['selection']
return HttpResponseRedirect('/doclistings')
else:
form = DropdownSelectionForm()
# d['genderselect'] = genderselect
s_name = request.session.get('selection')
d['userselection'] = s_name
spec = Specialization.objects.get(name=s_name)
doctors = Doctor.objects.filter(specialization = spec).order_by('-likes')
d['doctors'] = doctors
d.update({'form': form})
return render_to_response('meddy1/doclistings.html',d)
forms.py
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 template where I've the two dropdown
<select class="form-control" id="selection" name="selection">
<option><b>Find a Doctor...</b></option>
{% for value, text in form.selection.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<select class="form-control" id="genderdropdown" name="genderdropdown">
<option><b>Select a Gender</b></option>
{% for value, text in form.genderselect.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Search</button>
</span>
</div>
{% csrf_token %}
</form>
You should change the name="genderdropdown" attribute to match the form field name.
In other words, replace name="genderdropdown" with name="genderselect".

Categories

Resources