Friday, August 24, 2012

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

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

On Fri, Aug 24, 2012 at 3:00 PM, Sergiy Khohlov <skhohlov@gmail.com> wrote:
 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 <bdmorrison@gmail.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.
>
>
>
> On Friday, August 24, 2012 3:01:32 AM UTC-7, Tom Evans wrote:
>>
>> On Fri, Aug 24, 2012 at 9:37 AM, Barry Morrison <bdmor...@gmail.com>
>> wrote:
>> > 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!
>>
>> 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
>
> --
> 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-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment