2012-02-20 89 views
1

我建立了一个django自定义评论应用程序,使用django评论本身。我遵循https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/这封信,我有两个问题,一个是我的自定义评论实例不给content_object。自定义django评论

所以,当我尝试以下方法我什么也没得到

c = CommentWithFile.object.get(id)=1 
c.content_object 

两个,我的意见没有采取上传我添加的自定义注释形式中的文件。

我想要做的另一件事是通过邮件通知某个特定用户的列表,每次有人对某个特定主题发表评论时,但我想在通知中添加一个网址或主题的标题评论发布了,我怎么能这样做?

我的自定义评论模型。

def upload_path(instance, filename): 
    return '/'.join(['uploads','comment_archives', filename]) 

class CommentWithFile(Comment): 

    comment_file = models.FileField(max_length=255, upload_to=upload_path, 
     blank=True, null=True) 
    notify = models.BooleanField(_("Notificar usuarios")) 

    @property 
    def fileobject(self): 
     if self.comment_file: 
      return FileObject(self.comment_file.path) 
     return None 

我的自定义模型形式

class CommentFormWithFile(CommentForm): 
    comment_file = forms.FileField(label=_("Archivo"), required=False) 
    notify = form.BooleanField(label=_("Notificar usuarios")) 

    def get_comment_model(self): 
     # Use our custom comment model instead of the built-in one. 
     return CommentWithFile 

    def get_comment_create_data(self): 
     # Use the data of the superclass, and add in the title field 
     data = super(CommentFormWithFile, self).get_comment_create_data() 
     data['comment_file'] = self.cleaned_data['comment_file'] 
     data['notify'] = self.cleaned_data['notify'] 
     return data 

而在初始化的.py

from apps.comments.models import CommentWithFile 
from apps.comments.forms import CommentFormWithFile 

def get_model(): 
    return CommentWithFile 

def get_form(): 
    return CommentFormWithFile 

我commentwithfile

from apps.comments.models import CommentWithFile 

class CommentWithFileAdmin(admin.ModelAdmin): 
    pass 

admin.site.register(CommentWithFile, CommentWithFileAdmin) 

林全光照的管理文件g django 1.3.1,并有django通知为了通知用户的评论。

谢谢大家!

==== ==== UPDATE

下面是评论表单模板

{% load comments i18n %} 
<form action="{% comment_form_target %}" method="post">{% csrf_token %} 
    {% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %} 
    {% for field in form %} 
    {% if field.is_hidden %} 
     <div>{{ field }}</div> 
    {% else %} 
     {% if field.errors %}{{ field.errors }}{% endif %} 
     <p 
     {% if field.errors %} class="error"{% endif %} 
     {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}> 
     {% if field.label == 'Comentario' or field.label == 'Archivo' %} 
     {{ field }} 
     {% endif %} 
     </p> 
    {% endif %} 
    {% endfor %} 

    <div class="actions"> 
    <input type="hidden" name="next" value="{{ request.path }}" /> 
    <input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" /> 
    <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" /> 
    </div> 
</form> 

的继承人我如何使这种形式在其他模板

{% get_comment_form for archive as form %} 
    <h4>Comentar</h4> 

    <div class="main_comment" id="comment_form"> 
     {% render_comment_form for archive %} 
    </div> 
+0

你可以发布你的模板和我们可能需要的一切来重现你的情况吗? TYIA :) – jpic 2012-02-20 17:50:11

回答

2

2种东西为您的系统工作所必需:

  1. 该标签具有允许文件上传的enctype属性,例如<form enctype="multipart/form-data" method="post" action="">,或者浏览器将不发送

  2. 的形式实例化与两个request.POST和request.FILES,例如文件form = form_class(request.POST, request.FILES)。否则FileField将没有任何价值。

所以真的发生了什么,从你的话题缺失是:

  1. 形式HTML

  2. 视图蟒蛇代码,提示:务必检查请求。文件那里BTW

对于我来做一个也许更具体的答案。

+0

你好,我已经测试了在表单enctype =“multipart/form-data中添加以下内容,并且仍然没有上传文件......为什么我应该为这个评论创建一个视图,当所有的Im这样做是通过在模型中添加一个新字段来定制django注释。 – maumercado 2012-02-22 21:26:47

+0

第2点是怎么回事?“该表单与request.POST和request.FILES都是实例化的,例如form = form_class(request.POST,request.FILES)。否则FileField将不会有任何价值“https://docs.djangoproject.com/en/dev/topics/http/file-uploads/ – jpic 2012-02-23 13:54:05

+0

但我应该改变这种情况下没有instatiated这样?我不认为它必须直接在django代码上完成,我在django.contrib.comments视图代码中看到的是this data = request.POST.copy(),然后沿着行form = form = comments.get_form()(target, data = data)所以它显式地使用了完整的请求 – maumercado 2012-02-23 16:11:54