E
E
ever4st2020-10-30 21:57:55
Django
ever4st, 2020-10-30 21:57:55

How to fix django file display?

When I started writing forms in create.html, the display of the page as an html document changed to the display of the document's code. please help

the views.py file

from django.shortcuts import render
from django.http import HttpResponse
from .models import hot, post
from .forms import postform

# Create your views here.
def index(request):
    return render(request, 'main/index.html', {'title': 'О ПРОЕКТЕ'})

def me(request):
    return render(request, 'main/me.html', {'title': 'Я'})

def news(request):
    return render(request, 'main/news.html', {'hn': hot.objects.all(), 'sn':post.objects.order_by('-id'), 'title': 'НОВОСТИ'})

def create(request):
    return render(request, 'main/create.html', {'title': 'Новый пост'}, {'form': postform()})


forms.py file
from .models import post
from django.forms import ModelForm, TextInput


class postform(ModelForm):
    class Meta:
        model = post
        fields = ['title', 'text']
        widgets = {
            'title': TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Название'
            }),
            'text': TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Текст'
            })
        }


create.html file
{% extends 'main/base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block header %}
    <a href="{% url 'feed' %}" class="first upnav"> НОВОСТИ </a>|
    <a href="" class="upnav"> ОБЩЕНИЕ</a>
{% endblock %}

{% block body %}
    <form method="post">
        {% csrf_token %}
        {{ form.title }}<br>
        {{ form.text }}<br>
        <button type="submit" class="btn btn-success">Добавить</button>
    </form>
{% endblock %}

5f9c620271311980040984.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noremorse_ru, 2020-10-31
@ever4st

return render(request, 'main/create.html', {'title': 'Новый пост'}, {'form': postform()})

you passed 2 arguments to the context, instead of one {'title': 'New post', form': postform()}
ps read how to properly name classes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question