Пример #1
0
def add_photo(request): 
    c = {}
    c.update(csrf(request))
    if request.method == 'POST':
        from django.template.defaultfilters import slugify
        photo_dict = {                   'title' : request.POST['title'], 
                                        'slug_title' : request.POST['slug_title'],
                                        'pub_datetime': dt.datetime.now(),
                                        'posted_by': request.POST['posted_by'],
                                        'description' : request.POST['description'],
                                        'image' : request.FILES,
        }
        form = PhotoForm(request.POST
                        ,request.FILES)
        if form.is_valid():
            form.save()
            return redirect('/photos/add/complete/')
        else:
            form.error = "Photo form did not validate. Please contact the administrator about this."
            return render_to_response('poly_assoc_website/photo_add.html', {'form' : form }, RequestContext(request))
    if request.method == 'GET':                         
        form = PhotoForm(auto_id=True)
        try:
            return render_to_response('poly_assoc_website/photo_add.html', {'form' : form }, RequestContext(request))
        except TemplateDoesNotExist:
            raise Http404() 
Пример #2
0
def photo_edit(request, photo_id):
    c = {}
    c.update(csrf(request))   
    if request.method == 'GET':
        photo = Photo.objects.get(id=photo_id)
        form = PhotoForm(instance=photo)
        try:
            return render_to_response('poly_assoc_website/photo_edit.html',
                                      {'form' : form,
                                        'photo':photo}, 
                                      RequestContext(request))
        except TemplateDoesNotExist:
            raise Http404()
    if request.method == 'POST':
        photo = Photo.objects.get(id=photo_id)
        form = PhotoForm(request.POST,request.FILES, instance=photo)
        if form.is_valid():
            form.save()
            return redirect('/my-items/%d/' % photo.posted_by.id)
        else:
            form.error = "Photo did not validate. Maybe some field are missing"
            return render_to_response('poly_assoc_website/photo_edit.html', {'form' : form }, RequestContext(request))