T
T
trytrytry2013-12-22 21:29:07
Django
trytrytry, 2013-12-22 21:29:07

How to properly write a drop down list using a model?

I need to write a drop-down list and an ok button, when clicked, the selected item gets into the database. Here is the code for the drop down list.

index.html
<form action="file.php" method="get">
<select name="linksNav" size="1">
<option value="{% url 'useappl' useappl_id=p.id %}" selected >{{p.calendar}}</option>
</select>
</form>

As I understand it, I need to write two methods in views:
def index(request): if request.user.is_authenticated(): t=Application.objects.get(status1=True)

    return render(request, 'useappl/index.html', { 't' : t } )
else:
    return HttpResponseRedirect(reverse('account.views.login'))

and another method that will just process the selected element
def useappl(request, useappl_id):
    t=Application.objects.get(pk=useappl_id)
    return HttpResponseRedirect(reverse('account.views.login'))

And now I don't know what url to write in index.html. Help me please.
And in useappl, by what name should request.post["***"] be taken?
I just don't have file.php. This is from the found code.
Probably the useappl method is not correct if I don't use post. When I pass it to the template. Please help me figure it out.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SilentSokolov, 2013-12-22
@SilentSokolov

Read about forms in django , where examples explain how to make a drop-down list and a simple field and file upload.
The example that you found in general is from another steppe, django is the python language, and file.php is the php language, you will not find them in one project.
Here is a very stretched example of using modelform :
models.py

class Application(models.Model):
    brand = models.ForeignKey('Brand', blank=True, null=True)

forms.py
class ApplicationForm(ModelForm):
    class Meta:
        model = Application
        fields = ['brand']

views.py
def useappl(request, useappl_id):
    if request.method == 'POST': 
        form = ApplicationForm(request.POST)
        if form.is_valid(): 
            # Обработка
            form.save() # сохранение  модели
            return HttpResponseRedirect('/thanks/')
    else:
        form = ApplicationForm()

    return render(request, 'contact.html', {'form': form})

contact.html
{{ form }} # выведет форму, с выпадающем списком из брендов, останется прилепить кнопку

T
trytrytry, 2013-12-22
@trytrytry

by assignment should use the model

F
fordiesel, 2016-03-04
@fordiesel

STATUS_CHOICES = (
('new', u'New case'),
('published', u'Published'),
('decline', u'Declined'),
('duplicate', u'Duplicate'),
)
class Post(models.Model):
status = models.CharField(u'Status', max_length=30, choices=STATUS_CHOICES)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question