Hi Quentin,

The @processor_for decorator is totally suited to manage form validation, 
have a look at mezzanine.forms.page_processors for a working example.

Some advices on your code, though:
 - I don't think that “if request.method == ("post" or "POST")” really does 
what you think it does. You’d better use “if request.method == "post" or 
request.method =="POST"“ or better “if request.method == "POST"“ or even 
better, get rid of this if and create the form as “form = 
DocForm(request.POST or None, request.FILES or None)”
 - Your model has a ForeignKey for an UploadPage model and you’re creating 
it with “uploadpage = page.id”. It would be wiser to use the page instance 
instead of its id.
 - Upon successful validation, you redirect the visitor to “request.path + 
'?submitted=true'” which is basically the same view but you never handle 
this GET parameter in your page processor. Is it by design?

(also, why don't you use quotation marks or diacritics in your docstrings?)

Cheers,


Le mercredi 3 juin 2015 18:07:25 UTC+2, Quentin M a écrit :
>
> [Update] I just found the "page_processor" so I will test if it is the 
> solution, but not sure if the page_processor will make it with the 
> "ForeignKey" attribute...
> [Edit] I tested with the page processor but it don't works ... can 
> page_processor validate a form ?
>
> page_processors.py : 
> from django import forms
> from django.http import HttpResponseRedirect
> from mezzanine.pages.page_processors import processor_for
> from .models import Document, UploadPage
> from django.db import models
>
> class DocForm(forms.Form):
>     docfile = models.FileField(upload_to=mezzSettings.PDT_VERSION, 
> help_text="Ajouter un document en annexe.")
>
> @processor_for(UploadPage)
> def uploadpage_form(request, page):
>     form = DocForm()
>     if request.method == ("post" or "POST"):
>         form = DocForm(request.POST, request.FILES)
>         if form.is_valid():
>             Document(docfile = request.FILES['docfile'], uploadpage = 
> page.id).save()
>             redirect = request.path + "?submitted=true"
>             return HttpResponseRedirect(redirect)
>     return {"form":form} 
>
> Le mercredi 3 juin 2015 11:13:56 UTC+2, Quentin M a écrit :
>>
>> Firstly Hello ! I am Mezzanine newbie and I would like to have some help. 
>> I know there are many similar questions but no one answers my question.
>> I have a model called "Document" assiociated to a custom page 
>> "UploadPage" :
>>
>> models.py :
>> # -*- coding: utf-8 -*-
>> from django.db import models
>> from django.utils.translation import ugettext_lazy as _
>>
>> from mezzanine.conf import settings as mezzSettings
>> from mezzanine.pages.models import Page, RichText
>>
>>
>>
>> class UploadPage(Page, RichText):
>>      """Page pour l upload des annexes"""
>>      class Meta:
>>          verbose_name = _("Upload page")
>>          verbose_name_plural = _("Upload pages")
>>
>> class Document(models.Model):
>>      """Modele representatif d un document annexe"
>>      docfile = models.FileField(upload_to=mezzSettings.PDT_VERSION)
>>      uploadpage = models.ForeignKey("UploadPage", related_name="files")
>>
>>
>> admin.py :
>> # -*- coding: utf-8 -*-
>> from django.contrib import admin
>>
>> from mezzanine.core.admin import TabularDynamicInlineAdmin
>> from mezzanine.pages.admin import PageAdmin
>>
>> from quentinApp.models import UploadPage, Document
>>
>> class DocumentInline(TabularDynamicInlineAdmin):
>>      model = Document
>>
>> class UploadPageAdmin(PageAdmin):
>>      inlines = (DocumentInline,)
>>
>> admin.site.register(UploadPage, UploadPageAdmin)
>>
>> This code works but I would like to add a form field in this page 
>> (displayed in the site) that allow an user to upload a file (no worries 
>> about the security it's an intranet site).
>> I have already tested with a "form page" with field form but I don't like 
>> the way it stores the documents (create an unique "jksdfdfjkhomp" key 
>> folder for the document).
>> Should I add a Custom form field wich upload a file and create the page 
>> related object "Document"... In the documentation I found the "fields_for" 
>> function so the custom form must be the key, but I have no ideas how to 
>> make it. 
>> Ps: I have some difficulties in expressing myself in English, do not 
>> hesitate to ask me if I was not clear.
>>
>> Cordially,
>> Quentin
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to