0

I used {{ form }} as seen here

template.html

<h4>Change your details</h4><br>

<form id="edit_form" method='post'>

    {% csrf_token %}

    {{ form }}

    <div class='section group'>
        <input id="update_details_button" type='submit' class='btn btn-primary wide' value='Change'/>
    </div>

</form>

views.py

def user_view(request, is_admin):
    user = request.user

    form = myForm()

    if request.method == 'POST' and is_admin:
        form = myForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data

            user.name = data['name']
            user.country = data['country']
            user.save()

            messages.success(request, 'You have successfully updated your details.')

    return render(request, 'mysite/user.html', {
        'user': user,
        'is_admin': is_admin,
        'form': form,
    })

My form is as followed

class myForm(forms.Form):
    name = forms.CharField(
        label="Name",
        widget=forms.TextInput(attrs={'placeholder': 'Name'}))
    country = CountryField(blank_label='(select country)')

    def __init__(self, *args, **kwargs):
        super(myForm, self).__init__(*args, **kwargs)

The name field displayed fine on the page but there's no sign of the CountryField, could someone point out the error? The code compiled fine and gives no error while server is running.

Alex Lisovoy
  • 5,767
  • 3
  • 27
  • 28
Aithusa
  • 169
  • 1
  • 14

3 Answers3

4

CountryField is a model field, not a form field. You're supposed to add it to your user model, in which case a modelform based on that model will automatically generate a country field. Since it looks like you have actually added a field called country to the User model, that's where you should be using CountryField.

However, for reference, to do it manually on a non-model form is slightly more complicated:

from django_countries import widgets, countries

class myForm(forms.Form):
    country = forms.ChoiceField(widget=CountrySelectWidget, choices=countries)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks a lot, i am still new to Django. I have got a few more questions now, hope that you might be able to help with them. 1. The drop down menu is displaying on the form now, but then it doesn't have the blank default message. Do I still put `blank_label='(select country)'` in models.py? 2. The flag is not displaying next to the widget, what might be the problem? Thanks! :) – Aithusa Apr 27 '15 at 13:01
  • Also, I am not sure where to use `COUNTRIES_FIRST` to show some countries at the top of the list. – Aithusa Apr 27 '15 at 13:12
  • Just to update on the problems I had and share my solution: 1) `country = CountryField(blank_label='(select country)')` You put `blank_label` in the model you added the CountryField 2) You put COUNTRIES_FIRST in the settings.py and specify the country code that you would like to show at the top of the drop down menu Hope this helps other people who have the same questions :) – Aithusa Jul 23 '15 at 08:39
  • Code for COUNTRIES_FIRST: `from django_countries.conf import settings settings.COUNTRIES_FIRST = [ 'GB' ]` The country will appear underneath the blank label – Aithusa Jul 23 '15 at 08:53
  • There is a little mistake in your answer. It has to be `widget=widgets.CountrySelectWidget` ... I cannot edit your answer myself, which helped me today! – Fuchsi Feb 24 '23 at 08:57
1

In fact it's simpler: https://pypi.org/project/django-countries/#custom-forms

from django_countries.fields import CountryField


class MyForm(forms.Form):
    country = CountryField(blank=True).formfield()

Guillaume Lebreton
  • 2,586
  • 16
  • 25
0

If you have installed django_country and it added in installed app than no need to make it from just use like this

{% load countries %}
 <select name="country">
  {% countries %}
   <option value="{{ code }}">{{ name }}</option>
  {% endcountries %}
 </select>
Abhi
  • 67
  • 7