Django - 将用户的TextField和FileField保存在一起

时间:2016-10-20 12:24:24

标签: django django-models django-views

我正在制作一个基本博客,其中包含帖子的标题,文字和图片字段。但是,当我尝试在一个视图中将模型的TextField和FileField一起保存时,我继续收到以下错误。

错误。

AttributeError at /new/
'Post' object has no attribute '_committed'
Request Method:     POST
Request URL:    http://127.0.0.1:8000/new/
Django Version:     1.9.8
Exception Type:     AttributeError
Exception Value:    
'Post' object has no attribute '_committed'
Exception Location:     /usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py in pre_save, line 309
Python Executable:  /usr/bin/python3
Python Version:     3.5.2
Python Path:    
['/home/appu/Django/Colesk',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/home/appu/.local/lib/python3.5/site-packages',
 '/usr/local/lib/python3.5/dist-packages',
 '/usr/lib/python3/dist-packages']

邮政课程。

class Post(models.Model):

    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length = 200)
    text = models.TextField()
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    created_date = models.DateTimeField(default = timezone.now)
    published_date = models.DateTimeField(blank = True, null = True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()
    def __str__(self):
        return self.title

邮政表格。

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields = ('title' ,'text','docfile')

获取帖子的视图

def new_post(request):

    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit = False)
            post.docfile = Post(docfile = request.FILES['docfile'])
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk = post.pk)
    else:
        form = PostForm()
    return render(request, 'core/post_edit.html', {'form' : form})

Post的HTML模板。

{% extends 'core/main.html' %}
{% block content %}

<h1>New Post</h1>
    <form method="POST" class="post-form" enctype="multipart/form-data">{% csrf_token %}
        {{form.as_p}}
        <button type="submit" class="save btn btn-default">Save</button>
    </form>

{% endblock %}

我正在尝试将文本和图像保存在一起,以便我可以访问它进行显示。如何纠正错误或是否有其他方法可以实现此目的?

1 个答案:

答案 0 :(得分:0)

问题是,在这一行:

$(document).off('click', '.defender').on('click','.defender', function () {

您正在将 UIView *banner = [[UIView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, 60)]; banner.backgroundColor = [UIColor blueColor]; UIImageView *imgBanner=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, banner.frame.size.width, banner.frame.size.height)]; [imgBanner setImage:@"img.png"]; imgBanner.contentMode = UIViewContentModeScaleAspectFit; [banner addSubView:imgBanner]; [self.view addSubview:banner]; 实例本身与docfile字段相关联。文件字段应包含File

类型的对象

参考:Django: Copy FileFields