Y
Y
y_o_l_k_i_n___e_g_o_r2021-05-30 08:46:20
Django
y_o_l_k_i_n___e_g_o_r, 2021-05-30 08:46:20

How to send context to django models?

The form:

class CreatePostForm(ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'text', 'spoiler']
        widgets = {
            'title': TextInput(attrs={
                'class': 'new-form-input',
                'placeholder': 'Header...'
            }),
            'text': Textarea(attrs={
                'class': 'comment-form',
                'placeholder': 'Text...'
            }
            ),
        }


Model:
class Post(models.Model):
    spoiler = models.ImageField(upload_to='user_images/')
    likes = models.IntegerField()
    title = models.CharField(max_length=200)
    text = models.TextField()
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    views = models.IntegerField()
    create_date = models.DateTimeField(auto_now_add=True)

    def get_absolute_url(self):
        return f'news/{self.id}'

    def __str__(self):
        return self.title


HTML form code:
<center>

    <div class="logo">
        <span class="re">Authorization</span> account<br>
    </div>
    {% if error %}
    <br>
    <div class="error">
        <span class="re" style="font-size: 20px;">Error</span><br><br>
        <span>{{ error }}</span>
    </div>
    {% endif %}
    <form method="post">
        {% csrf_token %}
        {{ form.username }}
        {{ form.password }}

        <p style="color: white;">You don't have account? - <a href="{% url 'registration' %}">registration</a></p>
        <button type="submit">Authorization </button>
        <br>
    </form>
</center>


Question:
I need to pass the error variable to the form, but how if only the form variable is passed in the class?

60b326a441038009109633.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question