I'm new to Django. And recently am working on a website that need to show data from mongoDB and collect people's answers by offering forms. And now I got stuck with saving data from form to mongoDB.
I want people to only choose one answer in the dropdown form. Here is the html:
<table>
<form action="/reply/" method="POST" >
<td>
<select name = "reply">
<option value="#">Choose</option>
<option value="support">Support</option>
<option value="against">Against</option>
<option value="related">Related</option>
<option value="irrelated">Irrelated</option>
</select>
<input type="submit" value="OK!">
</form>
</table>
And here is my view
def labeling(request):
form = request.POST
if form.is_valid():
db.label.insert({
reply : form,
Post_ID : reequest.GET['id']
})
db.label.update
return HttpResponseRedirect("")
I have created collection named "label".
I have been working on this problems for a long time...I'll appreciate if someone could help me...
This may work for you :-
Your views.py :-
def labeling(request):
if request.method == 'POST':
form = FormClassName(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.reply = form.cleaned_data['reply']
instance.postId = form.cleaned_data['postId']
instance.save()
data = {'success':True,'msg':'Store user data successfully'}
return HttpResponse(json.dumps(data),content_type="application/json")
else:
data = {'success':False,'msg':'Not Store user data successfully'}
return HttpResponse(json.dumps(data),content_type="application/json")
Your forms.py :-
class FormClassName(ModelForm):
class Meta:
model = modelName
fields = [
"reply",
"postId",
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.