how to get image url from django form

Xristos Xristoou saxri89 at gmail.com
Sat Apr 15 15:36:51 EDT 2017


i have create a simple django form where the authentication user can select one of personal images where have upload before and i want do something with that request in my views.py.

but i dont know how to take the image url from that request in my view because first i using request.user in the form to take only self user images and i dont know how to continue and finaly i take that images url.

the form work great show me only the self user images

any idea ?

here the code

views.py

   @login_required(login_url="login/")
    def carlist(request):
        Myform = MyModelForm(user=request.user)
        return render(request,'about.html',{'Myform':Myform})

select django form :

class MyModelForm(ModelForm):
    def __init__(self, *args, **kwargs):
        # extract "user" from kwrags (passed upon form init)
        if 'user' in kwargs:
            self.user = kwargs.pop('user')
        super(MyModelForm, self).__init__(*args, **kwargs)
        # generate the choices as (display, value). Display is the one that'll be shown to user, value is the one that'll be sent upon submitting (the "value" attribute of <option>)
        choices = MyModel.objects.filter(user=self.user).values_list('upload', 'id')
        self.fields['upload'].widget = Select(choices=choices)

    class Meta:
        model = MyModel
        fields = ('upload',)

html :

    <form class="" action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
      {{ Myform}}
    <input type="submit" name="" value="Submit">



More information about the Python-list mailing list