Not sure how to describe issue/behavior with Form -- so here's code & YouTube video

49 views
Skip to first unread message

Barry Morrison

unread,
Aug 24, 2012, 4:37:13 AM8/24/12
to django...@googlegroups.com
Code: http://dpaste.org/az8Nb/
Video http://www.youtube.com/watch?v=XFSb044UkPs&feature=youtu.be

so first action, I click + which adds new, hitting cancel, takes me back to the view displaying all the news events
next action, I hit + again, adds new news event...this time I browse for an image, again, I click cancel, takes me back to the view of all news events, but you see now an empty news event has been created
next action, I hit + again, add a new image, hit submit. success_url takes me go the view of all events, this time it created the form I submitted and created another empty one

The function in views.py "add_news_item" is not set up in the URL, but it does support the cancel on the form action. The video posted is using that view for the URL.  With the 'CreateView' in the URL, canceling the form will create an empty field in the database. 

Any help/guidance would be greatly appreciated!!

Thanks!

Tom Evans

unread,
Aug 24, 2012, 6:01:32 AM8/24/12
to django...@googlegroups.com
I've not watched your video. I also have no idea what crispy forms
are, or what django form attributes are created using that syntax.
However…

Your model definition allows an empty form to be valid - none of those
fields are required AFAICT. Therefore, submitting an empty form using
the cancel button is being allowed to proceed. Therefore your check on
whether "cancel" was submitted is not working.

Put some debug here:

http://dpaste.org/az8Nb/#l28

Output what "submit" is, and all values in request.POST.

Cheers

Tom

Barry Morrison

unread,
Aug 24, 2012, 5:31:26 PM8/24/12
to django...@googlegroups.com, teva...@googlemail.com
For ease of use, let's take the cancel out of the equation perhaps. 

Url to create a new News Event:
    ### Create News Article
    url(r'^account/news/add/$', login_required(CreateView.as_view(
        model=NewsArticle,
        form_class=NewsForm,
        success_url=("/account/news"),
        template_name="account/news_admin.html")),
        name="admin-add-news"),


Url that gets called when I chose to insert an image into the WYSIWYG editor (and calls 'upload_photos' that has the NewsArticle.objects.create())

    ### Redactor.js WYSIWYG Editor
    ###
    url(r"^uploads/$", "web_site.News.views.upload_photos", name="upload_photos"),
    url(r"^recent/$", "web_site.News.views.recent_photos", name="recent_photos"),

and the views themselves:

@csrf_exempt
@require_POST
@login_required
def upload_photos(request):
    images = []
    for f in request.FILES.getlist("file"):
        obj = NewsArticle.objects.create(upload=f, is_image=True)

        images.append({"filelink": obj.upload.url})
    return HttpResponse(json.dumps(images), mimetype="application/json")


@login_required
def recent_photos(request):
    images = [
    {"thumb": obj.upload.url, "image": obj.upload.url}
    for obj in NewsArticle.objects.filter(is_image=True).order_by("-date_created")[:20]
    ]
    return HttpResponse(json.dumps(images), mimetype="application/json")


I've tried to set a .save(commit=False) on obj -- that doesn't work.

I realize the behavior is call 'upload_photos' and it'll create the object in the database, but on any other event other than submission of the form, I want that object to no longer exist.  I guess I want it to ONLY get created upon form submission.

Sergiy Khohlov

unread,
Aug 24, 2012, 6:00:32 PM8/24/12
to django...@googlegroups.com
Hello Barry !
I would like to say that your are not novice. now and your code is
really clear.
Could you please move string #92 up ? super should be called before
other field initialization.
look like your prev activity is stored after init process and those
data is saved at the save operation.
try to print form to log after pressing cancel button

thanks , serge

2012/8/25 Barry Morrison <bdmor...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/cf1oUHRglAcJ.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Barry Morrison

unread,
Aug 24, 2012, 6:57:58 PM8/24/12
to django...@googlegroups.com
Serge,
Thank you. 
#92 is for the crispy form. 

The crispy-form is the actual form, but the form is using a WYSIWYG editor http://redactorjs.com/ and when I go to browse for an image it calls url 'photo-uploads' which calls the function 'upload_photos' in the view. 

That view is necessary for redactor.js to get the image(s) from it's API via json, so I understand why the view creates the object, but doing so, it creates an empty field in the database. 

I had separated the models, so I had an Article model and an ArticleImage model, but then redactor.js failed to work (unsure how to create the relationship in the crispy form or any form for that matter). 

Thanks,
Barry

Sergiy Khohlov

unread,
Aug 25, 2012, 8:40:10 AM8/25/12
to django...@googlegroups.com
super(NewsForm, self).__init__(*args, **kwargs) this one shouold be
first string in the form constructor..... not at the end.

2012/8/25 Barry Morrison <bdmor...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages