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<javascript:>> 
> 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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to