I would like to use generic views to allow users to upload images,
with the image linked ( via ForeignKey ) to the user id.
The following is my code: ( a link to dpaste is here: http://dpaste.com/71693/
)

# models.py

class PressRelease(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    pub_date = models.DateField(auto_now_add=True)
    author = models.CharField(max_length=100)
    images = models.ImageField(upload_to='user_media')
    user = models.ForeignKey(User)

    def __unicode__(self):
        return self.title

class PressReleaseCRUD(ModelForm):
    class meta:
        model = PressRelease
        exclude = ['pub_date','user']


# urls.py


press_create_dict = {
# just a side note: on the template : press_create.html, call the form
just by using {{ form }}
    'form_class': PressReleaseCRUD, #
    #'model': PressRelease,
    'template_name':'press/press_create.html',
    'post_save_redirect':'/press/list/',
    #'extra_context':{'forms': PressReleaseCRUD}
    #'extra_context':{'pub_date': set_pub_date()}
}

urlpatterns = patterns('',
    (r'^detail/(?P<pid>\d+)/$','press.views.detail'),

    #using generic views -> list/detail generic views
    (r'^list/$', 'django.views.generic.list_detail.object_list',
press_list_dict),
    (r'^detail/(?P<object_id>\d+)/$',
'django.views.generic.list_detail.object_list', press_detail_dict),

    # testing django.views.generic.create_update.create_object
    (r'^create/$', 'django.views.generic.create_update.create_object',
press_create_dict),

    #using generic views redirect
    #(r'$','django.views.generic.simple.redirect_to',{'url':'/press/
list/'})

)




# views.py
@login_required
def image_upload(request):
    if request.user.is_authenticated():
        form = PressReleaseCRUD(request.POST, request.FILES)
        username = request.user
        if request.method == 'POST':
            form_upload = form
            if form_upload.is_valid():
                img_instance=form_upload.save(commit=False)
                img_instance.user=username
                img_instance.save()
                #form_upload.save()
                return HttpResponseRedirect('/tag_images/') # here may
be now add tags
        else:
            form_upload = PressReleaseCRUD()
        variables = RequestContext(request ,
{'form':form,'username':username,})
        return render_to_response('upload_image.html',variables )

    else:
        return HttpResponseRedirect('/login/')

# press_create.html
<form action="" method="post">
<p>{{ form.as_p }}</p>
<input type="submit" value="Submit" />
</form>

What happens now is that on http://localhost:8000/press/create/ , the
form presented given the current code is that i received an error:
NoneType' object is not callable.

However, if i were to use 'model': PressRelease, instead of the
current 'form_class': PressReleaseCRUD, in urls.py, i will received
a page with the form rendered, however the 'user' field will be a
options field with all the available users.

So my questions are:
1) My question is, using django generic views, how do i customize the
model's form before saving it such that the image uploaded is linked
via ForeginKey to the respective user? ( without the user required to
select from a list of possible users from a select box )

2) For django's generic view, what is the general method in
customizing the model's form ( assumin we use form_class as the
required model as stated in
http://docs.djangoproject.com/en/dev/ref/generic-views/#create-update-delete-generic-views
) ?
Take for instance in my example, i have a image_upload function in
views.py ; how do i call the image_upload function from
press_create_dict in  urls.py ?  Or is there another method for
customizing?

3) how do i fix the NoneType' object is not callable error?

Thank you all, i know this is a lengthy post.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to