I
I
ImLeftHanded2018-10-24 19:17:16
Django
ImLeftHanded, 2018-10-24 19:17:16

How to solve "'NoneType' object has no attribute 'group'" and "Article matching query does not exist" error?

Added the ability to delete objects, which led to errors
1) Now, when I just edit the article or add a new one, the error
'NoneType' object has no attribute 'group'
takes off 2) Up to a certain point, deletion worked fine, specifically until go ahead 9 (10, 11, etc.)
Now, when I delete the object, the error
Article matching query does not exist
view.py takes off

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) 
        btnname = matchobj.group()
        pri_key = btnname[-1]
        if btnname:
            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>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ImLeftHanded, 2018-10-24
@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})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question