Я пытался сделать форму для создания поста, генерируя с помощью django widget-tweaks поля для заполнения на bootstrap. Однако при запуске django выдал следующую ошибку:
AttributeError at /blog/post/create 'str' object has no attribute 'as_widget'
Пишет что ошибка находится в views.py в строке
return render(request, 'blog/post_create_form.html', context={'form': 'form'})
Мой views.py:
rom django.shortcuts import render
from django.shortcuts import redirect
from django.shortcuts import get_object_or_404
from django.views.generic import View
from .models import Post, Tag
from .utils import ObjectDetailMixin
from .forms import TagForm, PostForm
# Create your views here.
def posts_list(request):
posts = Post.objects.all()
return render(request, 'blog/index.html', context={'posts': posts})
class PostDetail(ObjectDetailMixin, View):
model = Post
template = 'blog/post_detail.html'
# def get(self, request, slug):
#post = get_object_or_404(Post, slug__iexact = slug)
#return render(request, 'blog/post_detail.html', context={'post': post})
class PostCreate(View):
def get(self, request):
form = PostForm()
return render(request, 'blog/post_create_form.html', context={'form': 'form'})
def post(self, request):
bound_form = PostForm(request.POST)
if bound_form.is_valid():
new_post = bound_form.save()
return redirect(new_post)
return render(request, 'blog/post_create_form.html', context={'form': 'bound_form'})
class TagDetail(ObjectDetailMixin, View):
model = Post
template = 'blog/tag_detail.html'
#def get(self, request, slug):
#tag = Tag.objects.get(slug__iexact=slug)
#return render(request, 'blog/tag_detail.html', context={'tag': tag})
class TagCreate (View):
def get(self, request):
form = TagForm()
return render(request, 'blog/tag_create.html', context={'form': form})
def post(self, request):
bound_form = TagForm(request.POST)
if bound_form.is_valid():
new_tag = bound_form.save()
return redirect(new_tag)
return render(request, 'blog/tag_create.html', context={'form': bound_form})
def tags_list(request):
tags = Tag.objects.all()
return render(request, 'blog/tags_list.html', context={'tags': tags})
.html файл страницы создания, которая создает ошибку:
{% extends 'blog/base_blog.html'%}
{% block title %}
Create Post - {{ blog.super }}
{% endblock %}
{% block content %}
<form class="" action="{% url 'post_create_url'%}" method="post">
{% csrf_token %}
{% load widget_tweaks %}
{% for field in form %}
<div class="mb-5">{% render_field field class="form-control -lg" placeholder=field.label %}
{% if field.errors %}
<div class="mb-5 alert alert-danger alert-dismissible fade show" role="alert">{{field.errors}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-outline-light" >Create new tag</button>
</form>
{% endblock %}
Также по каким-то причинам он указывает в нулевую строчку файла base.html. Сам base.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>
{% block title %}
Blog Engline
{% endblock title %}
</title>
</head>
<body style="background: gray">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/">Django.test</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'posts_list_url' %}">Blog<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/blog/post/create">Create Post</a>
</li>
<li class="nav-item">
<a class="nav-link " href="/blog/tags" tabindex="-1" aria-disabled="false">Tags</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container mt-5">
<div class="row">
<div class="col-6 offset-md-2">
{% block content%}
There is no any content for you
{% endblock %}
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
context={'form': 'form'}
. Мне кажется, Вы не хотели передавать строку. Попробуйте так:context={'form': form}