• Как решить ошибку "'NoneType' object has no attribute 'group'" и "Article matching query does not exist "?

    @ImLeftHanded Автор вопроса
    def manage_articles(request):
        if not request.user.is_staff:
                return redirect('/')
        ArticleFormSet = modelformset_factory(Article, fields=('title', 'priceMazut', 'priceSPBT',
                                                               'priceDTl', 'priceDTm', 'priceDTz'))
        if request.method == 'POST':
            enurl = urllib.parse.urlencode(request.POST) 
            matchobj = re.search(r'del_btn\d+', enurl)
            if matchobj is not None:
                btnname = matchobj.group()
                pri_key = btnname[7:]
                if btnname:
                    Article.objects.get(pk=pri_key).delete()
                    formset = ArticleFormSet(request.POST, request.FILES)
                    formset.save()
                    return redirect('/?$')
            else:
                formset = ArticleFormSet(request.POST, request.FILES)
                if formset.is_valid():
                    formset.save()
                    return redirect('/?$')
        else:
            formset = ArticleFormSet()
        return render(request, "table/manage_articles.html", {"formset": formset})
    Ответ написан
    Комментировать
  • Как осуществить удаление из modelformset?

    @ImLeftHanded Автор вопроса
    Разобрался с удалением, но теперь, когда я добавляю новые строки или редактирую имеющиеся, выдает ошибку вместо сохранения
    AttributeError at /table/edit/
    'NoneType' object has no attribute 'group'


    view.py
    def manage_articles(request):
        if not request.user.is_staff:
                return redirect('/')
        ArticleFormSet = modelformset_factory(Article, fields=('title', 'priceMazut', 'priceSPBT',
                                                               'priceDTl', 'priceDTm', 'priceDTz'))
        if request.method == 'POST':
            enurl = urllib.parse.urlencode(request.POST)  
            matchobj = re.search(r'del_btn\d', enurl) 
            pri_key = matchobj.group()[-1]
            if matchobj:
                   query = Article.objects.get(pk=pri_key).delete()
                   formset = ArticleFormSet(request.POST, request.FILES)
                   if formset.is_valid():
                       formset.save()
                       formset = ArticleFormSet(queryset=Article.objects.all())
                       return redirect('/?$')
            else:
                formset = ArticleFormSet(request.POST, request.FILES)
                if formset.is_valid():
                    formset.save()
    
                    return redirect('/?$')
        else:
            formset = ArticleFormSet()
        return render(request, "table/manage_articles.html", {"formset": formset})


    {{ formset.management_form }}
        {% for form in formset %}
            {{ form.id }}
            {{ form.instance.id }}
            {{ form.DELETE }}
           <tr class="edt {% if forloop.first  %} shadow {% endif %}"
                   {% if forloop.counter|divisibleby:"2" %}style="background-color: white"
                   {% else %} style="background-color: #F2F2F2"
                   {% endif %}>
    
               <td>  {{ form.title }} </td>
               <td>{{ form.priceMazut }} </td>
               <td>{{ form.priceSPBT }} </td>
               <td>{{ form.priceDTl }} </td>
               <td>{{ form.priceDTm }} </td>
               <td>{{ form.priceDTz }} </td>
               <td><input type="submit" name="del_btn{{ form.instance.id }}" value="Delete"/></td>
               <td class="last"></td>
           </tr>
    Ответ написан
    Комментировать