I can't get the selected student in widget tweaks render_field using CreateView










0














I am using the following code in views.py:



 class MatriculaCreateView(CreateView):
template_name = "website/matricula.html"
model = CursoPeriodoEstudante
form_class = MatriculaMembroForm
success_url = reverse_lazy("website:lista_estudantes")

def get_context_data(self, **kwargs):
context = super(MatriculaCreateView, self).get_context_data(**kwargs)
context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
context['pk'] = self.kwargs['pk']
return context


I create the following in forms.py:



 class MatriculaMembroForm(forms.ModelForm):

class Meta:
# Modelo base
model = CursoPeriodoEstudante

# Campos que estarão no form
fields = [
'estudante',
'cursoPeriodo'

]


And finally in the template I created this page:



 % extends "website/_layouts/base.html" %

% load widget_tweaks %

% block title %Matricula de Membros% endblock %

% block conteudo %
<div class="container mt-5">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">Matrícula de Membros</h5>
<p class="card-text">
Complete o formulário abaixo para matricular
um <code>Membro</code> em um evento.
</p>
<p>Membro: estudante </p>
<form method="post">
<!-- Não se esqueça dessa tag -->
% csrf_token %

<!-- Estudante -->
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Estudante</span>
</div>
% render_field form.estudante class+="form-control" %
</div>
<hr>
<!-- Curso -->
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Evento</span>
</div>
% render_field form.cursoPeriodo class+="form-control" %
</div>
<hr>
<div class="text-right">
<a href="% url 'website:lista_estudantes' %" class="btn btn-outline-primary">Voltar</a>
<button class="btn btn-primary">Enviar</button>
</div>
</form>
</div>
</div>
</div>
</div>
% endblock %


The problem is, when the page opens, I want the field "Estudante" already filled with the one I selected in the view through the get_context_data (This field can be fixed). But the field always comes like this:printscreen from page. How can I fix to come with the student like this. What can I do? Thank you!



Ps.: I tried to put the following, but nothing happened...



 class MatriculaCreateView(CreateView):
template_name = "website/matricula.html"
model = CursoPeriodoEstudante
form_class = MatriculaMembroForm
success_url = reverse_lazy("website:lista_estudantes")

def get_initial(self):
return 'estudante': Estudante.objetos.filter(id=self.kwargs['pk'])

def get_context_data(self, **kwargs):
context = super(MatriculaCreateView, self).get_context_data(**kwargs)
context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
context['pk'] = self.kwargs['pk']
return context









share|improve this question




























    0














    I am using the following code in views.py:



     class MatriculaCreateView(CreateView):
    template_name = "website/matricula.html"
    model = CursoPeriodoEstudante
    form_class = MatriculaMembroForm
    success_url = reverse_lazy("website:lista_estudantes")

    def get_context_data(self, **kwargs):
    context = super(MatriculaCreateView, self).get_context_data(**kwargs)
    context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
    context['pk'] = self.kwargs['pk']
    return context


    I create the following in forms.py:



     class MatriculaMembroForm(forms.ModelForm):

    class Meta:
    # Modelo base
    model = CursoPeriodoEstudante

    # Campos que estarão no form
    fields = [
    'estudante',
    'cursoPeriodo'

    ]


    And finally in the template I created this page:



     % extends "website/_layouts/base.html" %

    % load widget_tweaks %

    % block title %Matricula de Membros% endblock %

    % block conteudo %
    <div class="container mt-5">
    <div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <div class="card">
    <div class="card-body">
    <h5 class="card-title">Matrícula de Membros</h5>
    <p class="card-text">
    Complete o formulário abaixo para matricular
    um <code>Membro</code> em um evento.
    </p>
    <p>Membro: estudante </p>
    <form method="post">
    <!-- Não se esqueça dessa tag -->
    % csrf_token %

    <!-- Estudante -->
    <div class="input-group mb-3">
    <div class="input-group-prepend">
    <span class="input-group-text">Estudante</span>
    </div>
    % render_field form.estudante class+="form-control" %
    </div>
    <hr>
    <!-- Curso -->
    <div class="input-group mb-3">
    <div class="input-group-prepend">
    <span class="input-group-text">Evento</span>
    </div>
    % render_field form.cursoPeriodo class+="form-control" %
    </div>
    <hr>
    <div class="text-right">
    <a href="% url 'website:lista_estudantes' %" class="btn btn-outline-primary">Voltar</a>
    <button class="btn btn-primary">Enviar</button>
    </div>
    </form>
    </div>
    </div>
    </div>
    </div>
    % endblock %


    The problem is, when the page opens, I want the field "Estudante" already filled with the one I selected in the view through the get_context_data (This field can be fixed). But the field always comes like this:printscreen from page. How can I fix to come with the student like this. What can I do? Thank you!



    Ps.: I tried to put the following, but nothing happened...



     class MatriculaCreateView(CreateView):
    template_name = "website/matricula.html"
    model = CursoPeriodoEstudante
    form_class = MatriculaMembroForm
    success_url = reverse_lazy("website:lista_estudantes")

    def get_initial(self):
    return 'estudante': Estudante.objetos.filter(id=self.kwargs['pk'])

    def get_context_data(self, **kwargs):
    context = super(MatriculaCreateView, self).get_context_data(**kwargs)
    context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
    context['pk'] = self.kwargs['pk']
    return context









    share|improve this question


























      0












      0








      0







      I am using the following code in views.py:



       class MatriculaCreateView(CreateView):
      template_name = "website/matricula.html"
      model = CursoPeriodoEstudante
      form_class = MatriculaMembroForm
      success_url = reverse_lazy("website:lista_estudantes")

      def get_context_data(self, **kwargs):
      context = super(MatriculaCreateView, self).get_context_data(**kwargs)
      context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
      context['pk'] = self.kwargs['pk']
      return context


      I create the following in forms.py:



       class MatriculaMembroForm(forms.ModelForm):

      class Meta:
      # Modelo base
      model = CursoPeriodoEstudante

      # Campos que estarão no form
      fields = [
      'estudante',
      'cursoPeriodo'

      ]


      And finally in the template I created this page:



       % extends "website/_layouts/base.html" %

      % load widget_tweaks %

      % block title %Matricula de Membros% endblock %

      % block conteudo %
      <div class="container mt-5">
      <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
      <div class="card">
      <div class="card-body">
      <h5 class="card-title">Matrícula de Membros</h5>
      <p class="card-text">
      Complete o formulário abaixo para matricular
      um <code>Membro</code> em um evento.
      </p>
      <p>Membro: estudante </p>
      <form method="post">
      <!-- Não se esqueça dessa tag -->
      % csrf_token %

      <!-- Estudante -->
      <div class="input-group mb-3">
      <div class="input-group-prepend">
      <span class="input-group-text">Estudante</span>
      </div>
      % render_field form.estudante class+="form-control" %
      </div>
      <hr>
      <!-- Curso -->
      <div class="input-group mb-3">
      <div class="input-group-prepend">
      <span class="input-group-text">Evento</span>
      </div>
      % render_field form.cursoPeriodo class+="form-control" %
      </div>
      <hr>
      <div class="text-right">
      <a href="% url 'website:lista_estudantes' %" class="btn btn-outline-primary">Voltar</a>
      <button class="btn btn-primary">Enviar</button>
      </div>
      </form>
      </div>
      </div>
      </div>
      </div>
      % endblock %


      The problem is, when the page opens, I want the field "Estudante" already filled with the one I selected in the view through the get_context_data (This field can be fixed). But the field always comes like this:printscreen from page. How can I fix to come with the student like this. What can I do? Thank you!



      Ps.: I tried to put the following, but nothing happened...



       class MatriculaCreateView(CreateView):
      template_name = "website/matricula.html"
      model = CursoPeriodoEstudante
      form_class = MatriculaMembroForm
      success_url = reverse_lazy("website:lista_estudantes")

      def get_initial(self):
      return 'estudante': Estudante.objetos.filter(id=self.kwargs['pk'])

      def get_context_data(self, **kwargs):
      context = super(MatriculaCreateView, self).get_context_data(**kwargs)
      context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
      context['pk'] = self.kwargs['pk']
      return context









      share|improve this question















      I am using the following code in views.py:



       class MatriculaCreateView(CreateView):
      template_name = "website/matricula.html"
      model = CursoPeriodoEstudante
      form_class = MatriculaMembroForm
      success_url = reverse_lazy("website:lista_estudantes")

      def get_context_data(self, **kwargs):
      context = super(MatriculaCreateView, self).get_context_data(**kwargs)
      context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
      context['pk'] = self.kwargs['pk']
      return context


      I create the following in forms.py:



       class MatriculaMembroForm(forms.ModelForm):

      class Meta:
      # Modelo base
      model = CursoPeriodoEstudante

      # Campos que estarão no form
      fields = [
      'estudante',
      'cursoPeriodo'

      ]


      And finally in the template I created this page:



       % extends "website/_layouts/base.html" %

      % load widget_tweaks %

      % block title %Matricula de Membros% endblock %

      % block conteudo %
      <div class="container mt-5">
      <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
      <div class="card">
      <div class="card-body">
      <h5 class="card-title">Matrícula de Membros</h5>
      <p class="card-text">
      Complete o formulário abaixo para matricular
      um <code>Membro</code> em um evento.
      </p>
      <p>Membro: estudante </p>
      <form method="post">
      <!-- Não se esqueça dessa tag -->
      % csrf_token %

      <!-- Estudante -->
      <div class="input-group mb-3">
      <div class="input-group-prepend">
      <span class="input-group-text">Estudante</span>
      </div>
      % render_field form.estudante class+="form-control" %
      </div>
      <hr>
      <!-- Curso -->
      <div class="input-group mb-3">
      <div class="input-group-prepend">
      <span class="input-group-text">Evento</span>
      </div>
      % render_field form.cursoPeriodo class+="form-control" %
      </div>
      <hr>
      <div class="text-right">
      <a href="% url 'website:lista_estudantes' %" class="btn btn-outline-primary">Voltar</a>
      <button class="btn btn-primary">Enviar</button>
      </div>
      </form>
      </div>
      </div>
      </div>
      </div>
      % endblock %


      The problem is, when the page opens, I want the field "Estudante" already filled with the one I selected in the view through the get_context_data (This field can be fixed). But the field always comes like this:printscreen from page. How can I fix to come with the student like this. What can I do? Thank you!



      Ps.: I tried to put the following, but nothing happened...



       class MatriculaCreateView(CreateView):
      template_name = "website/matricula.html"
      model = CursoPeriodoEstudante
      form_class = MatriculaMembroForm
      success_url = reverse_lazy("website:lista_estudantes")

      def get_initial(self):
      return 'estudante': Estudante.objetos.filter(id=self.kwargs['pk'])

      def get_context_data(self, **kwargs):
      context = super(MatriculaCreateView, self).get_context_data(**kwargs)
      context['estudante'] = Estudante.objetos.filter(id=self.kwargs['pk'])
      context['pk'] = self.kwargs['pk']
      return context






      django forms django-widget-tweaks






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 18:22

























      asked Nov 12 at 12:03









      Alex Steeve

      34




      34






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Override the get_initial() method of CreateView to set initial values for your model:



          def get_initial(self):
          return 'estudante': Estudante.objects.get(id=self.kwargs['pk'])





          share|improve this answer






















          • I tried (you can see below) but nothing happened... :(
            – Alex Steeve
            Nov 12 at 18:22


















          0














          I got it!
          I should change the view to get, instead of filter (a query).



          Thanks!






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261793%2fi-cant-get-the-selected-student-in-widget-tweaks-render-field-using-createview%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Override the get_initial() method of CreateView to set initial values for your model:



            def get_initial(self):
            return 'estudante': Estudante.objects.get(id=self.kwargs['pk'])





            share|improve this answer






















            • I tried (you can see below) but nothing happened... :(
              – Alex Steeve
              Nov 12 at 18:22















            0














            Override the get_initial() method of CreateView to set initial values for your model:



            def get_initial(self):
            return 'estudante': Estudante.objects.get(id=self.kwargs['pk'])





            share|improve this answer






















            • I tried (you can see below) but nothing happened... :(
              – Alex Steeve
              Nov 12 at 18:22













            0












            0








            0






            Override the get_initial() method of CreateView to set initial values for your model:



            def get_initial(self):
            return 'estudante': Estudante.objects.get(id=self.kwargs['pk'])





            share|improve this answer














            Override the get_initial() method of CreateView to set initial values for your model:



            def get_initial(self):
            return 'estudante': Estudante.objects.get(id=self.kwargs['pk'])






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 at 10:23

























            answered Nov 12 at 15:11









            dirkgroten

            3,93011121




            3,93011121











            • I tried (you can see below) but nothing happened... :(
              – Alex Steeve
              Nov 12 at 18:22
















            • I tried (you can see below) but nothing happened... :(
              – Alex Steeve
              Nov 12 at 18:22















            I tried (you can see below) but nothing happened... :(
            – Alex Steeve
            Nov 12 at 18:22




            I tried (you can see below) but nothing happened... :(
            – Alex Steeve
            Nov 12 at 18:22













            0














            I got it!
            I should change the view to get, instead of filter (a query).



            Thanks!






            share|improve this answer

























              0














              I got it!
              I should change the view to get, instead of filter (a query).



              Thanks!






              share|improve this answer























                0












                0








                0






                I got it!
                I should change the view to get, instead of filter (a query).



                Thanks!






                share|improve this answer












                I got it!
                I should change the view to get, instead of filter (a query).



                Thanks!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 22:37









                Alex Steeve

                34




                34



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261793%2fi-cant-get-the-selected-student-in-widget-tweaks-render-field-using-createview%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto