django-views

Django request Post json

早过忘川 提交于 2019-12-18 14:47:16
问题 I try to test a view, I receive a json request from the IPad, the format is: req = {"custom_decks": [ { "deck_name": "deck_test", "updates_last_applied": "1406217357", "created_date": 1406217380, "slide_section_ids": [ 1 ], "deck_id": 1 } ], "custom_decks_to_delete": [] } I checked this in jsonlint and it passed. I post the req via: response = self.client.post('/library/api/6.0/user/'+ uuid + '/store_custom_dec/',content_type='application/json', data=req) The view return "creation_success":

How to get primary keys of objects created using django bulk_create

依然范特西╮ 提交于 2019-12-18 11:40:07
问题 Is there a way to get the primary keys of the items you have created using the bulk_create feature in django 1.4+? 回答1: 2016 Since Django 1.10 - it's now supported (on Postgres only) here is a link to the doc. >>> list_of_objects = Entry.objects.bulk_create([ ... Entry(headline="Django 2.0 Released"), ... Entry(headline="Django 2.1 Announced"), ... Entry(headline="Breaking: Django is awesome") ... ]) >>> list_of_objects[0].id 1 From the change log: Changed in Django 1.10: Support for setting

QuerySet, Object has no attribute id - Django

人走茶凉 提交于 2019-12-18 11:25:31
问题 I'm trying to fetch the id of certain object in django but I keep getting the following error Exception Value: QuerySet; Object has no attribute id. my function in views.py @csrf_exempt def check_question_answered(request): userID = request.POST['userID'] markerID = request.POST['markerID'] title=request.POST['question'] m = Marker.objects.get(id=markerID) u = App_User.objects.get(id=userID) print userID print markerID print title # userID='1' # markerID='1' # title='Hello' at = AttachedInfo

Populate a django form with data from database in view

时光毁灭记忆、已成空白 提交于 2019-12-18 11:07:52
问题 I have a form in my forms.py that looks like this: from django import forms class ItemList(forms.Form): item_list = forms.ChoiceField() I need to populate the item_list with some data from the database. When generated in HTML item_list should be something like: <select title="ItemList"> <option value="1">Select Item 1</option> <option value="2">Select Item 2</option> </select> The options values in my select statement will change almost every time since a variable in the query will often

django - what goes into the form action parameter when view requires a parameter?

前提是你 提交于 2019-12-18 11:02:31
问题 This is what I have: myview.py with a view that takes a parameter user : def myview(request, user): form = MyForm(request.POST) .... return render_to_response('template.html',locals(), context_instance=RequestContext(request)) The user gets passed through an url. urls.py : ... urlpatterns += patterns('myview.views', (r'^(?P<user>\w+)/', 'myview'), ) ... I also have a template.html with a form: <form name="form" method="post" action="."> ... </form> What goes in the the form action parameter

django - what goes into the form action parameter when view requires a parameter?

只谈情不闲聊 提交于 2019-12-18 11:02:23
问题 This is what I have: myview.py with a view that takes a parameter user : def myview(request, user): form = MyForm(request.POST) .... return render_to_response('template.html',locals(), context_instance=RequestContext(request)) The user gets passed through an url. urls.py : ... urlpatterns += patterns('myview.views', (r'^(?P<user>\w+)/', 'myview'), ) ... I also have a template.html with a form: <form name="form" method="post" action="."> ... </form> What goes in the the form action parameter

How to get the ID of the record just saved

╄→гoц情女王★ 提交于 2019-12-18 10:58:35
问题 I'm using Django 1.3 for one of my projects and I need to get the ID of a record just saved in the database. I have something like the code below to save a record in the database: n = MyData.objects.create(record_title=title, record_content=content) n.save() The ID of the record just saved auto-increments. Is there a way to get that ID and use it somewhere else in my code? 回答1: Use n.id after the save. See "Auto-incrementing primary keys". 回答2: It would be n.pk . To quote "Model.pk":

Class Based Views VS Function Based Views

不打扰是莪最后的温柔 提交于 2019-12-18 10:01:40
问题 I always use FBVs (Function Based Views) when creating a django app because it's very easy to handle. But most developers said that it's better to use CBVs (Class Based Views) and use only FBVs if it is complicated views that would be a pain to implement with CBVs. Why? What are the advantages of using CBVs? 回答1: The single most significant advantage is inheritance. On a large project it's likely that you will have lots of similar views. Rather than write the same code again and again, you

Disable a method in a ViewSet, django-rest-framework

元气小坏坏 提交于 2019-12-18 09:57:03
问题 ViewSets have automatic methods to list, retrieve, create, update, delete, ... I would like to disable some of those, and the solution I came up with is probably not a good one, since OPTIONS still states those as allowed. Any idea on how to do this the right way? class SampleViewSet(viewsets.ModelViewSet): queryset = api_models.Sample.objects.all() serializer_class = api_serializers.SampleSerializer def list(self, request): return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) def

Need to have a Required and Optional Fields in Django Formset

耗尽温柔 提交于 2019-12-18 09:39:50
问题 I created a formset that has maximum of 5 images to be attached; 1 - but I want the Validation to run only when the user has not attached any image (ValidationError('atleast 1 image is required')) , 2- This program is also not allowing the User to save when 1, or 2, or 3 images are attached, which I really need. So if there is 1 image or 2, that should be allowed to save. 3 - I also need to make the 1 radio-button to be selected by default, to make the selected image to be the one dispalyed