Quantcast

take select request from django from

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

take select request from django from

Xristos Xristoou

hello i have create a select django form with list of images for per user.

How to take that select request in my views.py ?

i have success only to create correct that select list but i need take that select request but i dont know how.

models.py

class MyModel(models.Model):
    user = models.ForeignKey(User, unique=True)
    upload = models.ImageField(upload_to='upload')

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">

for example Myform now have a list of user images that is correct but after from that i need the select images from the form.

can do it that with my code or not ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f9238e66-6174-4ca4-bcae-ae83dd9e7e74%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: take select request from django from

Camilo Torres
Hi,

I think you can do what you need with your current model and form and using FormSets, but then you should change your view and template:
https://docs.djangoproject.com/en/1.11/topics/forms/formsets/

On Sunday, April 16, 2017 at 3:41:04 PM UTC-4, Xristos Xristoou wrote:

hello i have create a select django form with list of images for per user.

How to take that select request in my views.py ?

i have success only to create correct that select list but i need take that select request but i dont know how.

models.py

class MyModel(models.Model):
    user = models.ForeignKey(User, unique=True)
    upload = models.ImageField(upload_to='upload')

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">

for example Myform now have a list of user images that is correct but after from that i need the select images from the form.

can do it that with my code or not ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ec7723ac-5c48-4745-9ebe-e16347598926%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...