Django项目,我需要有关我的模型的建议

时间:2019-01-10 23:24:50

标签: python mysql django

我需要用户能够将Post模型中的body和slug字段保存到数据库中,但是我遇到了问题。我的Post模型中有10个名为body1,body2等的字段。用户可以选择要保存的字段。如何保存身体场?如何在我的html代码中做到这一点?

class PostDig(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='dig')
    slug = models.SlugField(max_length=150, blank=True)
    body = models.TextField(blank=True)
    date_save= models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['-date_save']

models.py

class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    image = VersatileImageField(
        'Image',
        upload_to='media_my/',null=True,
        blank=True
    )
    title = models.CharField(max_length=150, db_index=True)
    slug = models.SlugField(max_length=150, unique=True, blank=True)
    tags = models.ManyToManyField('Tag', blank=True, related_name='posts')
    body1 = models.TextField(blank=True)
    body2 = models.TextField(blank=True)
    body3 = models.TextField(blank=True)
    body4 = models.TextField(blank=True)
    body5 = models.TextField(blank=True)
    body6 = models.TextField(blank=True)
    body7 = models.TextField(blank=True)
    body8 = models.TextField(blank=True)
    body9 = models.TextField(blank=True)
    body10 = models.TextField(blank=True)
    date_pub= models.DateTimeField(auto_now_add=True)

    def get_absolute_url(self):
        return reverse('post_detail_url', kwargs={'slug':self.slug})

    def get_update_url(self):
        return reverse('post_update_url', kwargs={'slug': self.slug})

    def get_delete_url(self):
        return reverse('post_delete_url', kwargs={'slug': self.slug})

    def save(self, *args, **kwargs):
        if not self.id:
            self.slug= gen_slug(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-date_pub']

HTML代码..

   {% extends 'face/base_face.html'%}

  {% block title %}
       {{ post.title }} - {{ block.super }}
   {% endblock %}


 {% block content %}
    <h1 class='mb-5'> </h1>
   <div class="card mb-5" style="width:400px; border-radius: 8px;">
  {% if post.image %}
  <img class="img-responsive" src="{{ post.image.thumbnail.400x400 }}"  style="border-radius: 8px;" alt="Card image" font-size:8px;>
  {% endif %}
 <div class="card-body">
  <h4 class="card-title">{{ post.title }}</h4>
  <p class="card-text">{{ post.body1 }} <a href="{{ post.get_savedig_url }}"><i class="fa fa-plus" aria-hidden="true"></i></a></p> 
  <p class="card-text">{{ post.body2 }}</p>
  <p class="card-text">{{ post.body3 }}</p>
  <p class="card-text">{{ post.body4 }}</p>
  <p class="card-text">{{ post.body5 }}</p>
  <p class="card-text">{{ post.body6 }}</p>
  <p class="card-text">{{ post.body7 }}</p>
  <p class="card-text">{{ post.body8 }}</p>
      <div class="card-footer text-muted">
      Author: {{ post.user }}

    </div>
 {% endblock %}

我希望用户能够将Post模型中的body字段保存在数据库中,并将其显示在用户个人资料中。

0 个答案:

没有答案