2012-02-20 87 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