django-views

Django file field update causing error even though not required

◇◆丶佛笑我妖孽 提交于 2019-12-24 06:48:09
问题 I have an app that serves to update certain fields of a model. There are 4 possible fields that could be updated: resolution, upload4, upload5, and upload6. The upload fields are NOT required. If I do not include the request.FILES line, the uploaded file will not be saved to the database, but it seems like because I've included it, I need to always upload the 3 files, even though they are not required. The exception I am getting is "MultiValueDictKeyError" on the POST. How can I fix this? I

How to compare lists and get total matching items count

本秂侑毒 提交于 2019-12-24 06:38:21
问题 Garage.cars: object garage has a FK of cars Person.carwishlst: object Person has FK of cars they would like. In Django how to I achieve the following, loop?... Get the total number of cars x the Person has which match the ones the Garage has. Outcome: i.e. Grange has 4 cars you want Hypothetical, but lets say Grange model has an FK cars = models.ManyToManyField(Cars) Person also has a FK cars_wishlist = models.ManyToManyField(Cars) 回答1: Assuming the cars and carwishlst are lists: You would do

Django UpdateView generic class

。_饼干妹妹 提交于 2019-12-24 06:17:14
问题 How can I access the object passed by the user inside a generic view class? In template, when the user clicks the link: <td><a href="{% url 'update_peon' pk=item.pk %}"><button class="btn btn-warning">Edit</button></a></td> this goes to urls.py: url(r'^update_peon/(?P<pk>\d+)$', views.UpdatePeon.as_view(), name='update_peon'), and my view: class UpdatePeon(generic.UpdateView): login_required = True template_name = 'appform/Peons/peon_form.html' model = Person form_class = PersonForm success

Deserialize POST request with subset of serializer fields in DRF

浪尽此生 提交于 2019-12-24 04:06:14
问题 I'm having a rather simple problem, but found a few solutions and couldn't stop wondering what the intended DRF approach would be. I have a (simplified) model and serializer like this: class CartProduct(models.Model): cart = models.ForeignKey('Cart', on_delete=models.CASCADE) product = models.ForeignKey('Product', on_delete=models.CASCADE) class CartProductSerializer(serializers.HyperlinkedModelSerializer): id = serializers.ReadOnlyField() product = ProductSerializer() class Meta: model =

Empty url in urls.py

若如初见. 提交于 2019-12-24 02:56:10
问题 I have a project with several views e.g. - index, contacts and about. And I want url www.aaa.net links to index view, www.aaa.net/about to about view and so on In project urls.py I wrote url(r'^$', include('mysite.urls')) In app urls.py I wrote url(r'^$',views.index,name='index'), url(r'about/$',views.about,name='about'), url(r'contacts/$',views.contacts,name='contacts'), But it works only with index view, about and contacts didn't work at all 回答1: Remove the $ at the end of url(r'^$',

Multiple images in django form with multiupload

为君一笑 提交于 2019-12-24 01:17:45
问题 I need to add multiple images in django form to one model. I did a research and for form outside of django I try to setup django-multiupload. My models.py: class Profile(models.Model): ... ... first = models.ImageField("first", upload_to='first') second = models.ImageField("second", upload_to='second') ... In forms.py: class AddForm(forms.ModelForm): first = MultiImageField(min_num=1, max_num=20) second = MultiImageField(min_num=1, max_num=4) In views.py: class UploadView(FormView): template

How do i fix this error in my Comments view in my Django app?

假如想象 提交于 2019-12-24 00:45:03
问题 I'm trying to develop an app in Django. At the moment I'm trying to create a comment section for the users to write and submit comments by using a form. I made a template which shows the info of a movie as well as a form through which users can write comments on the film. The problem is that when I write the comment and try to submit it this error shows up : IntegrityError at /myapp2/2/ NOT NULL constraint failed: myapp2_comentario.pelicula_id my Views.py def detallesPelicula(request,

How do you add a non-editable field to a custom admin form in Django

ⅰ亾dé卋堺 提交于 2019-12-23 18:09:48
问题 I am trying to add an editable=False field to a custom admin form, but I am getting an error: django.core.exceptions.FieldError: 'help_num' cannot be specified for Investigation model form as it is a non-editable field This is true, in my model I have it set as such: models.py help_num = models.CharField(max_length=17, unique=True, default=increment_helpdesk_number, editable=False) forms.py class HelpDeskModelForm(forms.ModelForm): class Meta: model = HelpDesk fields = [ "help_num", "help

How does template in django get the user object?

心不动则不痛 提交于 2019-12-23 17:42:36
问题 How does template get user object? In other words what process exactly during rendering passes user object to template? And what else is accessible in template? 回答1: Using the django.contrib.auth.context_processors.auth context processor, you can access the auth.User instance in your template. If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain these variables: user – An auth.User instance representing the currently logged-in user (or an AnonymousUser

Is there before_filter in django as in rails?

老子叫甜甜 提交于 2019-12-23 17:33:27
问题 Is there any feature available in django so that we can combine some filtering on all actions in the view of django, like before_filter: is available in rails. 回答1: No. before_, around_ and after_ filter concepts aren't present in Django, but it isn't hard to write your own functions to do the same thing. There are also signals and generic views that might accomplish what you're needing to do. 回答2: I'm still learning Rails but from what I've observed so far python decorators also seem to be