django-views

django - Get the set of objects from Many To One relationship

ぐ巨炮叔叔 提交于 2020-01-01 02:07:50
问题 Please have a look at these models: class Album(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class Photo(models.Model): album = models.ForeignKey(Album, default=3) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) How do I get the the set of photos for a particular album??? And how to get the

Get django object id based on model attribute

霸气de小男生 提交于 2019-12-31 17:58:35
问题 I have a basic model named "Places" which has this view: def view_index(request, place_name): The user will access that view with a URL like this one: http://server.com/kansas "kansas" is a value stored in a field named "name" inside the model "Places". The problem is that I can't figure out how to obtain the object id based just on the object name. Is there a way to do this? 回答1: Like this: place = Places.objects.get(name='kansas') print place.id 回答2: Since you only want id , you should only

Get django object id based on model attribute

我们两清 提交于 2019-12-31 17:57:50
问题 I have a basic model named "Places" which has this view: def view_index(request, place_name): The user will access that view with a URL like this one: http://server.com/kansas "kansas" is a value stored in a field named "name" inside the model "Places". The problem is that I can't figure out how to obtain the object id based just on the object name. Is there a way to do this? 回答1: Like this: place = Places.objects.get(name='kansas') print place.id 回答2: Since you only want id , you should only

Django 2, python 3.4 cannot decode urlsafe_base64_decode(uidb64)

纵然是瞬间 提交于 2019-12-31 10:39:10
问题 i am trying to activate a user by email, email works, encoding works, i used an approach from django1.11 which was working successfully. In Django 1.11 the following decodes successfully to 28, where uidb64 = b'Mjg' force_text(urlsafe_base64_decode(uidb64)) In django 2 (2, 0, 0, 'final', 0) the above code decode does not work and results in an error django.utils.encoding.DjangoUnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 1: invalid continuation byte. You passed in b'l

Django Class Based View for both Create and Update

倾然丶 夕夏残阳落幕 提交于 2019-12-31 09:11:50
问题 Say I want to create a Class Based View which both updates and creates an object. From a previous question I worked out I could do one of the following things: 1) Use 2 generic views CreateView and UpdateView which I think would mean having two URL's pointing to two different classes. 2) Use a class based view which inherits base View , which I think would mean having two URL's pointing to just 1 class (I created which inherits View ). I have two questions: a) Which is better? b) ccbv.co.uk

Django request get parameters

不想你离开。 提交于 2019-12-31 08:28:12
问题 In a Django request I have the following: POST:<QueryDict: {u'section': [u'39'], u'MAINS': [u'137']}> How do I get the values of section and MAINS ? if request.method == 'GET': qd = request.GET elif request.method == 'POST': qd = request.POST section_id = qd.__getitem__('section') or getlist.... 回答1: You can use [] to extract values from a QueryDict object like you would any ordinary dictionary. # HTTP POST variables request.POST['section'] # => [39] request.POST['MAINS'] # => [137] # HTTP

Django request get parameters

自作多情 提交于 2019-12-31 08:27:08
问题 In a Django request I have the following: POST:<QueryDict: {u'section': [u'39'], u'MAINS': [u'137']}> How do I get the values of section and MAINS ? if request.method == 'GET': qd = request.GET elif request.method == 'POST': qd = request.POST section_id = qd.__getitem__('section') or getlist.... 回答1: You can use [] to extract values from a QueryDict object like you would any ordinary dictionary. # HTTP POST variables request.POST['section'] # => [39] request.POST['MAINS'] # => [137] # HTTP

Django - DRF - dispatch method flow

谁都会走 提交于 2019-12-31 07:26:06
问题 I am working with DRF to build an API and I used a master class to do some validations to my class based views: class MasterClass(APIView): def dispatch(self, request, *args, ** response = super(FaveoAPIView, self).dispatch(request, *args, **kwargs) # I call super because I need access to request data. # <some validations here> # Return a JsonResponse with an error message if validations fails class MyView(MasteClass): def post(self, request, *args, **kwargs): # At this point request is:

How to render two form fields as one field in Django forms?

淺唱寂寞╮ 提交于 2019-12-31 05:58:08
问题 I'm working on Django application where I need to render a form differently. The form contains multiple fields related to a person. 1. Firstname 2. Lastname 3. Email 4. Address 5. City 6. Country 7. Phone 8. Pincode There are two flows in my application. In the first flow I can render all the form fields and save them when the user enters some data and submit. But in the second flow, I need to display only three fields as below. 1. Name - *Combination of Firstname and Lastname* 2. Email 3.

badly formed hexadecimal UUID error string for a UUID primary key

允我心安 提交于 2019-12-31 05:37:06
问题 I'm trying to get my table to load, but am getting this error, most likely because my primary key is a uuid. Here is my url paths. path('results/', views.results, name='results'), path('results/<uuid:pk>/', views.genre_book, name='books_genre_table'), Here is my views.py If there is one observation, we load the price table. If not, then we load a table with service descriptions. The user can then pick the service they are most interested and click through to the price table for that service.