Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] implement choices with Django framework #31

Open
vignesk70 opened this issue Feb 7, 2019 · 1 comment
Open

[Admin] implement choices with Django framework #31

vignesk70 opened this issue Feb 7, 2019 · 1 comment

Comments

@vignesk70
Copy link

I simply want to have a dropdown with country and save its code to neo4j as a string property. I am able to generate the drop down but it doesn't match what I want to achieve.

models.py

COUNTRY_CODE1=[("AF", 'Afghanistan'), ('BL', 'Albania'), ('DZ', 'Algeria')]

class Person(DjangoNode):
    uid_person = UniqueIdProperty()
    ind_name = StringProperty(max_length=100 , label='Enter your First Name',required=True)
    ind_last_name = StringProperty(max_length=100,null=True, label='Last Name',required=True)

ind_nationality = StringProperty(max_length=2,choices=COUNTRY_CODE1,label='Nationality',required=True) 
    class Meta:
        app_label = 'reg'

forms.py

    class Meta:
        model=Person
        fields = ['ind_name','ind_last_name', 'ind_nationality']#,'ind_country']#,'ind_address1','ind_address2','ind_address3', 'ind_town','ind_postcode','ind_state', 'ind_birthdate','ind_gender','ind_email','ind_countryiddcode','ind_contactnum']
        widgets = {

            'ind_name' : forms.TextInput(attrs={'size':50}),
            'ind_last_name' : forms.TextInput(attrs={'size':50}),
            'ind_nationality' : forms.Select(),
       
        }
        app_label = 'reg'

views.py

class PersonRegView(generic.FormView):
    template_name='reg/personreg.html'
    form_class=PersonRegForm
    success_url = 'index'

    def post(self, request, *args, **kwargs):

        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        print('view post',form)

   
        if (form.is_valid()): 
            return self.form_valid(form)
        else:
            #print(x)
            return self.form_invalid(form)

    def form_valid(self, form):
        self.object = form.save()
        #return HttpResponseRedirect(self.get_success_url())
        return HttpResponseRedirect(self.request.path_info)

    def form_invalid(self, form):
        
        return self.render_to_response(
            self.get_context_data(form=form)
            )

html template - using bootstrap4.

<div class="row justify-content-center">
        <div class="media-container-column col-lg-8" > <!-- data-form-type="formoid"-->
                <div data-form-alert="" hidden="">
                    {%bootstrap_form_errors form%}
                </div>

                <form class="mbr-form" action="#" method="post" data-form-title="PersonRegForm">

                  {% csrf_token %}

                  <div class="row row-sm-offset">
                    <div class = "col-sm-12"><h3> Personal Information </h3></div>

                      <div class="col-md-6 multi-horizontal" data-for="name">
                          {%bootstrap_field form.ind_name show_label=False%}
                      </div>
                     <div class="col-md-6 multi-horizontal" data-for="name">
                          {%bootstrap_field form.ind_last_name show_label=False%}
                      </div>
                      <div class="col-md-6 multi-horizontal" data-for="name">
                           {%bootstrap_field form.ind_nationality show_label=True %}
                       </div>

                  </div>


                    <span class="input-group-btn">
                        <button type="submit" class="btn btn-primary btn-form display-4">SEND FORM</button>
                    </span>
                </form>
        </div>

    </div>

My output shows that for the choices appears like this.

<select name="ind_nationality" class="form-control is-invalid" title="" required="" id="id_ind_nationality">
  <option value="">---------</option>

  <option value="A">F</option>

  <option value="B">L</option>

  <option value="D" selected="">Z</option>

</select>

I have two problems here . First one being the dropdown is incomplete I am assuming it should display

<option value="AF">Afghanistan</option>

Second problem is that when I try to save it throws an error invalid choice with whatever option selected.

I am able to successfully save it without dropdown choices just the first two fields.

@whatSocks whatSocks changed the title I am trying to implement choices with Django framework but am not able to get the correct values appearing. implement choices with Django framework Jul 6, 2021
@whatSocks whatSocks added this to To do in Django Admin Jul 27, 2021
@whatSocks whatSocks changed the title implement choices with Django framework [Admin] implement choices with Django framework Jul 27, 2021
@anuj9196
Copy link

@vignesk70 Were you able to solve this issue? I'm also facing the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants