-1

我是新的django,我正在嘗試創建博客。我已經有使用外鍵的發佈模型。用戶可以創建簡單的消息帖子,但我想走得更遠。我希望能夠爲每個帖子添加許多文件。我認爲我應該創建一個名爲Files with file area的模型。但我真的不知道如何使用文章和輸入來添加文件在帖子表單下。我聽說我需要使用formset,但我找不到任何這樣做的好例子。總之,如果你能給我一些教程(例子)或者給出一些建議如何啓動它,我將不勝感激。django創建多文件上傳表格

編輯:

我所做的一切就像例子,現在我有文件輸入形式,但這不是actualy上傳anytwhere。當我運行shell我沒有任何附件對象。繼承我的代碼。

型號:

from django.db import models 
from django.contrib.auth.models import User 
from multiupload.fields import MultiFileField 
import datetime 
class PostManager(models.Manager): 
    def published(self,): 
     return self.get_queryset().filter(active=True, pub_date__lt=datetime.datetime.now()) 

class Post(models.Model): 
    author=models.ForeignKey(User, verbose_name=u"Autor postu", blank=True, null=True) 
    active=models.BooleanField(verbose_name=u"Aktywnosc postu", default="False") 
    title=models.CharField(max_length=40, verbose_name=u"tytul") 
    lead=models.TextField(verbose_name=u"wprowadzenie") 
    body=models.TextField(verbose_name=u"Zawartosc postu") 
    pub_date=models.DateTimeField(verbose_name=u"Data publikacji", blank="True", null="True") 

    objects= PostManager() 

    class Meta: 
     verbose_name=u"post" 
     verbose_name_plural=u"posty" 

class Attachment(models.Model): 
    post = models.ForeignKey(Post, verbose_name=u"post") 
    file = models.FileField(upload_to='attachments', verbose_name=u"Plik") 
    file_name=models.TextField(verbose_name=u"Nazwa pliku") 

觀點:

from django.shortcuts import render, redirect, get_object_or_404 
from django.http import HttpResponse, HttpResponseRedirect 
from django.core.urlresolvers import reverse 
from django.contrib.auth import logout, login, authenticate 
from django.views.generic import ListView, DetailView 
from .models import Post 
import datetime 
from .forms import PostForm, CreationForm 
from django.views.generic.edit import FormView, CreateView 

class PostListView(ListView): 
    model= Post 

    def get_queryset(self,): 
     return Post.objects.published() 

class PostDetailView(DetailView): 
    model= Post 


def Edit(request, pk): 
    post= get_object_or_404(Post, pk=pk) 
    form= PostForm(instance=post) 
    context={ 
     'post': post, 
     'form': form, 
    } 
    return render(request,'posts/edit_post.html', context) 

def LogMeOut(request): 
    logout(request) 
    return HttpResponseRedirect('/posts') 

def log_form(request): 
    return render(request, 'posts/log_me_in.html', {}) 

def logged(request): 
    if request.method == 'POST':   
     user_name=request.POST.get('usr', '') 
     user_password=request.POST.get('pwd','') 
     user = authenticate(username=user_name, password=user_password) 
     if user is not None: 
      login(request,user) 
      return HttpResponseRedirect(reverse('post_list')) 
     else: 
      return HttpResponse('bieda') 


def apply_edit(request, post_id): 
    form=PostForm(request.POST) 
    if form.is_valid(): 
     instance=form.save(commit= False) 
     instance.author=request.user 
     instance.id=post_id 
     instance.pub_date=datetime.datetime.now() 
     instance.save(); 
     return HttpResponseRedirect(reverse('post_list')) 

def post_creation(request): 
    form=PostForm() 
    context= { 
     'form': form 
    } 
    return render(request, 'posts/post_creation.html', context) 

def my_posts(request): 
    user=request.user 
    object_list=user.post_set.all() 
    context={ 
     'object_list': object_list, 
    } 
    return render(request, 'posts/post_list.html', context) 




def create_post(request): 
    form=PostForm() 
    if request.method=='POST': 
     form=PostForm(request.POST) 
     if form.is_valid(): 
      instance=form.save() 
      instance.active=True; 
      instance.author=request.user 
      instance.pub_date=datetime.datetime.now() 
      instance.save(); 
    return HttpResponseRedirect(reverse('post_list')) 


class ContactView(CreateView): 
    model = Post 
    form_class = CreationForm 
    template_name = 'posts/post_creation.html' 
    success_url = 'posts/post_list.html' 

forms.py:

from django import forms 
from django.contrib.auth.models import User 
from .models import Post, Attachment 
import datetime 
from multiupload.fields import MultiFileField 

class PostForm(forms.ModelForm): 
    class Meta: 
     model= Post 
     exclude=['author', 'pub_date'] 

class CreationForm(forms.ModelForm): 
    class Meta: 
     model=Post 
     fields=['active', 'title', 'lead', 'body'] 



    files = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5) 


    def save(self, commit=True): 
     instance = super(ContactForm, self).save(commit=False) 
     instance.author=User 
     instance.pub_date=datetime.datetime.now() 
     instance.save() 
     for each in self.cleaned_data['files']: 
      Attachment.objects.create(file=each, post=instance, file_name=file.name) 

     return instance 

我認爲URL是罰款,所以我不要發佈。有人可以檢查它是否正確。如果不是那個有什麼問題。

回答