B
B
bimka2021-12-04 20:38:49
Django
bimka, 2021-12-04 20:38:49

Why doesn't it pass id to form.as_p?

I want to get the id of the object (post), for further use. To do this, the "id" field has been added to views.py for the JokesUpdate class, but for some reason it is not passed to form.as_p in the template. How can I get the id of an object?

jokes_update.html:

{% extends 'blog/base.html' %}

{% block content %}

<p>id = </p>  
{% for f in form %}
{{f}}
{% endfor %}
<br>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
  
    <input type="submit" value="Обновить шутку">
    <input type="button" onclick="window.location='{% url "index" %}';" value="Отмена">
</form>
{% endblock %}


views.py:
class JokesUpdate(UpdateView):
    model = Jokes
    fields = ('joke_text', 'id', 'joke_rating')
    success_url = reverse_lazy('index')
    template_name_suffix = '_update'


models.py:
class Jokes(models.Model):
    joke_text = models.TextField(
        verbose_name = "Текст шутки", max_length = 500
        )
    joke_rating = models.IntegerField(default = random.randint(1, 10))
    date_of_creation = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislava, 2021-12-07
@vladis005

You don't output id in template. The field can be rendered as {{ model.field }} where model should be the name of the model and field should be the field. For example id = {{ jokes.id }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question