自定义Django Crispy表单以用于格式化的用户输入(由用户格式化)

时间:2019-05-25 14:05:05

标签: django forms formatting

在Django项目中,我使用了松脆的表单来生成供用户输入标题和注释的表单。对于评论部分,我希望用户能够设置其输入格式(例如,添加粗体和斜体标签,以及嵌入youtube视频,因此请使用嵌入摘要)。这是典型的wordpress小部件(admin),它允许为任何文本输入设置文本格式。是否可以使用Django的Crispy表单执行此操作,如果可以,最好的前进方式是什么?我正在寻找有关文档,导入(与Django兼容的现有库?)的建议,因为我找不到任何建议,或者关于如何手动实现此方法的想法。例如具体来说,将在哪个“文件”中实施。 html页面的视图(当前) View of the current form and text input on the comment area.

我查看了其他问题-答案,但没有一个人专门回答。

这是我想添加到表单文本输入中的功能(仅针对内容/注释框)

Desired text input formatting options added

下面的当前 post_form.html 代码

{% extends "socialmedia/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
        {% csrf_token %}
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">Post your message</legend>
            {{form|crispy}}
        </fieldset>
    <div class="form-group">
        <button class="btn btn-outline-info" type="submit">Post</button>
    </div>
    </form>
</div>
{% endblock content %}

post_detail.html

{% extends "socialmedia/base.html" %}
{% block content %}

    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{object.author.profile.image.url}}">
    <div class="media-body">
        <div class="article-metadata">
      <a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
      <small class="text-muted">{{ object.date_posted|date:"F d, Y"}}</small>
      {% if object.author == user %}
        <div>
            <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.id %}"> Update </a>
            <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}"> Delete </a>
        </div>
      {% endif %}


        </div>
    <h2 class="article-title">{{ object.title }}</h2>
    <p class="article-content">{{ object.content }}</p>
  </div>
</article>
{% endblock content %}

models.py (与表格有关)

from django.db import models
from django.utils import timezone #don't forget to add this
from django.contrib.auth.models import User
from django.urls import reverse


class Post(models.Model):
    title=models.CharField(max_length=100)
    content=models.TextField(max_length=300)
    date_posted=models.DateTimeField(default=timezone.now) 

views.py (同样与表格有关)

from django.shortcuts import render,get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.models import User
from .models import Post #import the Models Post
from django.views.generic import (
    ListView,
    DetailView,
    CreateView,
    UpdateView,
    DeleteView
)


from django.http import HttpResponse
# Create your views here.



class PostListView(ListView):
    model = Post #what model to query in order to create the list
    template_name = 'socialmedia/home.html' 

    context_object_name = 'posts'
    ordering = ['-date_posted']#the minus sign before the -date_posted makes it newest first
    paginate_by = 10

class UserPostListView(ListView):
    model = Post #what model to query in order to create the list
    template_name = 'socialmedia/user_posts.html' 
    context_object_name = 'posts'
    paginate_by = 10    

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')



class PostDetailView(DetailView):
    model = Post 


class PostCreateView(LoginRequiredMixin,CreateView):

    model = Post 
    fields=['title','content']


    def form_valid(self,form):
        form.instance.author = self.request.user #set author to current loggged in user
        return super().form_valid(form)


class PostUpdateView(LoginRequiredMixin,UserPassesTestMixin, UpdateView):
        model = Post 
        fields=['title','content']

        def form_valid(self,form):
            form.instance.author = self.request.user 
            return super().form_valid(form)
        #this only lets the user of the post update the post.....
        def test_func(self):
            post = self.get_object() #this gets the current post
            if self.request.user == post.author:
                return True
            return False

class PostDeleteView(LoginRequiredMixin,UserPassesTestMixin, DeleteView):
    model = Post 
    success_url = '/'
    def test_func(self):
        post = self.get_object() #this gets the current post
        if self.request.user == post.author:
            return True
        return False

0 个答案:

没有答案