D
D
DennyD3142016-12-16 19:08:46
Django
DennyD314, 2016-12-16 19:08:46

How to pass the first object to the slug view?

Hello!
I think the task is quite trivial: when you go to a page with a menu, you need to automatically select the first menu item, highlight it as active, and display information on it.
I have implemented this logic:

  1. using a clean url (without a named list) go to the page
  2. Slug defaults to None
  3. The view checks if the Slug is None, if yes, then we try to take the first object from qs of all objects
  4. If it succeeds, then we pass it to the template
  5. If the slug is not equal to Non, then we pass it and the object with this slug to the template
  6. In the template, if the slug is passed, then we determine the active item by it, if not, then by forloop.counter

It seems to me that I was extremely clever with this task and could have been done many times easier.
URLs.py:
url(r'^excursions$', views.excursions, name='excursions'),
    url(r'^excursions/(?P<slug>[\S]+)/$', views.excursions, name='excursions')

views.py:
def excursions(request, slug=None):
    list_of_excursions = models.Excursion.objects.all()
    if slug == None:
        try:
            start_value = models.Excursion.objects.all()[0]
        except IndexError:
            return render(request, 'ex/excursions.html', {'Error_message': 'Извините, тут пока ничего нет'})
        else:
            return render(request, 'ex/excursions.html', {'list_of_excursions': list_of_excursions, 'first': start_value})
    else:
        start_value = get_object_or_404(models.Excursion, slug=slug)
        return render(request, 'ex/excursions.html', {'list_of_excursions': list_of_excursions, 'first':start_value, 'slug': slug})

sample:
{% if Error_message %}
    <p>{{Error_message}}</p>
{% else %}
    <div class="col-lg-2 col-md-4 col-xs-12">
        <ul class="nav nav-pills nav-stacked" >
            {% if slug %}
                {% for ex in list_of_excursions %}
                    {% if ex.slug == slug %}
                        <li class="active"><a href="{% url 'ex:excursions' ex.slug %}">{{ex.name}}</a></li>
                    {% else %}
                        <li><a href="{% url 'ex:excursions' ex.slug %}">{{ex.name}}</a></li>
                    {% endif %}
                {% endfor %}
            {% else %}
                {% for ex in list_of_excursions %}
                    {% if forloop.counter == 1 %}
                        <li class="active"><a href="{% url 'ex:excursions' ex.slug %}">{{ex.name}}</a></li>
                    {% else %}
                        <li><a href="{% url 'ex:excursions' ex.slug %}">{{ex.name}}</a></li>
                    {% endif %}
                {% endfor %}
            {% endif %}
        </ul>
    </div>
    <div class="col-lg-10 col-md-8 col-xs-12">
        <div class="center_place">
            <h1>{{first.name}}</h1><br>
            <p>{{first.text| safe}}</p>
        </div>
    </div>
{% endif %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-12-16
@sim3x

return render(request, 'ex/excursions.html', 
{'list_of_excursions': Excursion.objects.all() })

{% for ex in list_of_excursions %}
  {%  if forloop.first %}  
    первона
  {% endif %}

  <a href="{{ ex.get_absolute_url }}">ex.name</a>

{% else %}
  нифига нет екскурсий
{%endfor%}

r'^excursions$'write with a slash at the end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question