如何上传图片和创建视图

时间:2018-12-06 22:15:20

标签: django django-models django-forms django-templates django-views

我不明白如何在视图上添加此图像对象。 请帮助我。

模型

from django.db import models
from django.utils import timezone
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()

    image = models.ImageField(default='post.jpg', upload_to='image')

    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        super().save()


    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})

我对图像字段所做的操作是有效的。 我希望能够使用CreateView和LoginRequiredMixin上传图像文件,但无法正常工作-似乎在选择文件后该表单未绑定任何文件数据。这是视图的当前内容

观看次数

 class PostCreateView(LoginRequiredMixin, CreateView):
       model = Post
       fields = ['title', 'content','image']

       def form_valid(self, form):
           form.instance.author = self.request.user

           return super().form_valid(form)

任何人都想我会显示默认照片。我不明白该如何处理。我尽力了。

post_form.html

 {% extends "blog/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">Blog Post</legend>
            {{ form|crispy }}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" 
        type="submit">Post</button>
        </div>
       </form>
    </div>
   {% endblock content %}

home.html

  {% extends "blog/base.html" %}
  {% block content %}
     {% for post in posts %}
       <article class="media content-section">
      <img class="rounded-circle article-img" src="{{ 
       post.author.profile.image.url }}">
      <div class="media-body">
        <div class="article-metadata">
          <a class="mr-2" href="#">{{ post.author }}</a>
          <small class="text-muted">{{ post.date_posted|date:"F d, Y" }} 
          </small>
        </div>
        <h2><a class="article-title" href="{% url 'post-detail' post.id %}"> 
         {{ post.title }}</a></h2>
        <p class="article-content">{{ post.content }}</p>
        <img class="rounded-circle account-img" src="{{ post.image.url }}">
      </div>
     </article>
     {% endfor %}
   {% endblock content %}

命令行

OSError: [WinError 123] The filename, directory name, or volume label syntax 
is incorrect: 'C:\\Users\\Arvan\\Desktop\\django_project\\medi
a\\<MultiValueDict: {}>'
[07/Dec/2018 04:04:39] "GET /media/%3CMultiValueDict%3A%20%7B%7D%3E HTTP/1.1" 
500 80871

1 个答案:

答案 0 :(得分:0)

尝试在视图中代替当前的form_valid方法:

def form_valid(self, form):
    post = form.save(commit=False)
    post.save()
    return HttpResponseRedirect(reverse('redirect'))

并在您的模板中确保您的表单具有以下内容:

<form method="post" enctype="multipart/form-data">

键是提交文件的密码。