R
R
Ruslan Ezhgurov2015-10-17 13:44:39
Django
Ruslan Ezhgurov, 2015-10-17 13:44:39

What is wrong with my function?

I want to make one function to search for a specific article, search for articles in a specific category, and by tags. I get the article normally, on the rubric I get an empty list, but on tags in general 404 :( what's wrong?
Here are examples:
localhost/pishem-prostoi-parser - article
localhost/praktika-python - rubric
localhost/python - tag
Here is my function:

def article(request, alies):
    args = {}
    args.update(csrf(request))
    try:
        args['article'] = Article.objects.get(alies=alies)
    except Article.DoesNotExist:
        head_id = get_object_or_404(Heading, heading_alies=alies)
        args['article'] = Article.objects.filter(article_heading=head_id.id)
        if not args['article']:
            tag_id = get_object_or_404(Tag, tag_alies=alies)
            args['article'] = Article.objects.filter(tags=tag_id.id)
        else:
            pass
    args['heading'] = Heading.objects.all()
    return render_to_response('article.html', args)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nikita Shultais, 2015-10-17
@Oyaseo

If you have a Heading with the value praktika-python and there are no articles for this Heading, then an attempt is made to get a tag with the name praktika-python. Since your tag is called python, it is obvious that there is 404 here.
If you just drive in the python tag, then 404 is already on the line
PS.
You don't have to write like this, you get head, not head_id
right

heading = get_object_or_404(Heading, heading_alies=alies)

A
Alexander, 2015-10-17
@syschel

get_object_or_404() (source)
Find one object, if not found then show 404
You are looking for Heading, does not find it and throws an exception (raise Http404), no further code execution. There will also be an error if there is more than one object in the database. It will also spit out a 404 exception.

A
Artem Lisovsky, 2015-10-17
@torrie

article_heading=head_id.id is incorrect
if you have a foreign_key then objects should be compared
article_heading=head_id

R
Ruslan Ezhgurov, 2015-10-17
@Oyaseo

I redid it like this, as a result I get an empty list both in the headings and in the tags, although both the tag and the heading exist.

def article(request, alies):
    args = {}
    args.update(csrf(request))
    try:
        args['article'] = Article.objects.get(alies=alies)
    except Article.DoesNotExist:
        try:
            head_id = Heading.objects.get(heading_alies=head_id)
            args['article'] = Article.objects.filter(article_heading=head_id.id)
        except Heading.DoesNotExist:
            tag_id = get_object_or_404(Tag, tag_alies=alies)
            args['article'] = Article.objects.filter(tags_id=tag_id.id)
    args['heading'] = Heading.objects.all()
    return render_to_response('article.html', args)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question