django-views

Passing Instance to Django formset

瘦欲@ 提交于 2020-01-03 08:22:49
问题 How to pass a instance to the Django formset, The Scenario is like this. I have updated multiple rows by using a formset and, in a later stage i need to edit those values which i added earlier.(Editing) q = PaymentLines.objects.filter(pay_lines=project) formset = PayFormSet(prefix='payment', instance=q) 回答1: AuthorFormSet(queryset=Author.objects.all()) https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset 回答2: I have used it as follows the formset used is

Passing Instance to Django formset

一个人想着一个人 提交于 2020-01-03 08:22:13
问题 How to pass a instance to the Django formset, The Scenario is like this. I have updated multiple rows by using a formset and, in a later stage i need to edit those values which i added earlier.(Editing) q = PaymentLines.objects.filter(pay_lines=project) formset = PayFormSet(prefix='payment', instance=q) 回答1: AuthorFormSet(queryset=Author.objects.all()) https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset 回答2: I have used it as follows the formset used is

Ajax call from django template

百般思念 提交于 2020-01-03 06:57:05
问题 I have a django template which extends the base template that has code to load jquery in it. This template has a simple text box and I wanted to fetch the object through ajax. {% extends 'base.html' %} {% block content %} <form id="ajaxform"> <input type="text" name="first_name" id="name" /> <input type="submit" value="Submit" /> </form> <div id="dataDiv"> </div> <script> $('#ajaxform').submit(function(){ console.log('Form submitted'); $.get('{% url get_ajax_data %}', $(this).serialize()

DeleteView with 2 arguements post and user

旧时模样 提交于 2020-01-03 04:25:27
问题 I have a delete view with 2 conditions "post" and "user". The user requirement is fulfilled by self.object.user = self.request.user and post requirement is fulfilled by slug = self.kwargs['slug'] (I think this may be the culprit) Are my views correct? I am new to python please forgive any silly mistakes. Views.py class ProofDelete(LoginRequiredMixin, DeleteView): model = Proof def delete(self, *args, **kwargs): return super().delete(*args, **kwargs) def get_success_url(self, *args, **kwargs):

Django Rest get file from FileField url

巧了我就是萌 提交于 2020-01-03 04:22:09
问题 I have created a django rest model which includes a FileField. media = models.FileField(upload_to='media/%Y/%m/%d/', null=True, blank=True) I also implemented serializer and ListCreateApiView. There I can able to upload a file. On POST request rest server uploads the file in folder and returns me the url. However, on get request, server return json content with the url. If I use the url for get request, server respond Page Not Found . How to download the uploaded file using django rest? Do I

Django: query latest posts from different categories

▼魔方 西西 提交于 2020-01-03 03:28:10
问题 class Post(models.Model): title = ... category = models.ForeignKey(Category) date = ....... class Category(models.Model): title = .... On the main page i want to display 5 posts with latest dates, but all posts must be from different categories. There is 50 categoriese, for example. Is it possible? 回答1: from django.db.models import Max categories = Category.objects.annotate(most_recent=Max(post__date)).order_by('-most_recent')[:5] posts = list() for category in categories: posts.append

Django User Update Form and View

谁说胖子不能爱 提交于 2020-01-03 01:37:13
问题 I am very early on in my Django/Python development journey, most things I have been able to slowly figure out after a few hours/days of head scratching and trial/error. I now have the commonly asked question that I cannot get working correctly: How Do I Create a User Profile Update View/Form? I have hacked on several solutions from Stack Overflow, and just cannot figure out what I am doing wrong thus far. Here is the initial version of my poor attempt using Django 1.9: #forms.py class

django - reusing functions in many views

落花浮王杯 提交于 2020-01-02 19:37:11
问题 I have a bunch of functions that I created in some views that must be reused in many other views. Do I need to create a class and put those functions in a class? If yes how exactly has to be done in Django and then how do I call and initiate them in the new views? 回答1: Django views are just Python functions. You can call other Python functions from them just as you can from any other Python code. Put your functions into a .py file, import it, and invoke the functions. Of course, it may make

Class Based Generic UpdateView inline

落花浮王杯 提交于 2020-01-02 09:02:39
问题 I have the following models class Cv(models.Model): name = models.CharField(_('name'), max_length=250) objective = models.CharField(_('objective'), max_length=250) slug = models.SlugField(editable=False) class Position(models.Model): cv = models.ForeignKey(Cv, verbose_name=_('cv')) start = models.DateField(_('start')) end = models.DateField(_('end')) name = models.CharField(_('name'), max_length=250) currently_employed = models.BooleanField(_('currently employed')) sector = models

Class Based Generic UpdateView inline

ⅰ亾dé卋堺 提交于 2020-01-02 09:02:14
问题 I have the following models class Cv(models.Model): name = models.CharField(_('name'), max_length=250) objective = models.CharField(_('objective'), max_length=250) slug = models.SlugField(editable=False) class Position(models.Model): cv = models.ForeignKey(Cv, verbose_name=_('cv')) start = models.DateField(_('start')) end = models.DateField(_('end')) name = models.CharField(_('name'), max_length=250) currently_employed = models.BooleanField(_('currently employed')) sector = models