Django - Pass a model instance specified by template tags to the view

40 views
Skip to first unread message

Jack

unread,
Nov 8, 2017, 9:37:36 PM11/8/17
to Django users
On my HTML page, I have a list of Agent's (Agent is my custom User model).  Beside each agent is a 'Send Invite' button.  To create a `TeamInvitation`, I need to specify which Agent is attached to its `receiver_agent` field (a OneToOneField with Agent)

There are multiple Agents displayed on the HTML page, and they are listed in order with template tags.  I need to input something after `pk` at `receiver_agent = Agent.objects.get(pk = ???)`, but I don't know what to input.

views.py

    class InviteAgentSearchResults(ListView):
        model = Agent
        form_class = AgentSearchForm
        template_name = 'invite_agent_search_results.html'
        
        def get_queryset(self):
            # ... Code to find correct list of agents
        
        def post(self, request, *args, **kwargs):
            invite = TeamInvitation.objects.create(receiver_agent = Agent.objects.get(pk = ???))
            return HttpResponse('Invite successfully sent.')

HTML:

    {% for agent in agent_list %}
    <div class="agent">
    # ... Some code here

            <form method="post">  # The "Send Invite" button
                {% csrf_token %}
                <button class="button1"><span>Send Invite</span></button>
            </form>
    </div>
    {% endfor %}

Adam Simon

unread,
Nov 8, 2017, 11:24:29 PM11/8/17
to django...@googlegroups.com
--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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/bd0439e2-7fdd-4f7c-b6ce-4847d4a877c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
It seems to me that the quickest thing would be to make each agent a separate form. 

Alternatively, you could set up some JavaScript:

Add the agent.id to a hidden input,  separated by commas, and have that hidden field submitted via the form

Then on the backend separate out each ID and have the invites sent one by one. I would be happy to write some code if you wish.


--
--

Adam F. Simon, PhD
Calabasas, CA.

cell:      818-425-3719
home:   818-880-8405

Feel free to link w/ me: LinkedIn


Jack

unread,
Nov 9, 2017, 2:45:33 PM11/9/17
to Django users
There will be a lot of agents in the database, so would it violate the DRY principle if I were to write a form for each agent?  Plus it'll be super time consuming.

I'm going to try the hidden agent.id thing, I never thought about that.  I'll report back to see if I managed to get it working. 

Adam Simon

unread,
Nov 9, 2017, 3:41:07 PM11/9/17
to django...@googlegroups.com

Each form would be generated by a loop; still I would go with the hidden field. 

Look forward to seeing how it goes. 



For more options, visit https://groups.google.com/d/optout.

Jack

unread,
Nov 9, 2017, 11:12:58 PM11/9/17
to Django users
Ok I'm very stuck.  Thought very hard about it but can't see how I can add a hidden field into the HTML, and relay the specified instance of the hidden field back to the views... or am I completely off about something?

Also in regards to the looped form's.  If you look at my original code, I actually do have a <form> representing a button for each agent.  The resulting page looks like this (excuse the bad CSS).  I'm not sure if that is what you meant by having a form for each agent.

So if I understood you correctly, I do have a separate <form> input for each displayed agent; looped with {% for agent in agent_list %}.  The question is, how do I relay the correct agent linked to its 'Send Invite' button back to the views?

Adam Simon

unread,
Nov 9, 2017, 11:20:53 PM11/9/17
to django...@googlegroups.com

Hi Jack. I will write some more details when I get home. Have you ever used Ajax?

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Jack Zhang

unread,
Nov 9, 2017, 11:42:53 PM11/9/17
to django...@googlegroups.com
No I am not familiar with Ajax.  I only know a bit of JavaScript.

On Thu, Nov 9, 2017 at 6:19 PM, Adam Simon <add.si...@gmail.com> wrote:

Hi Jack. I will write some more details when I get home. Have you ever used Ajax?
On Thu, Nov 9, 2017 at 3:14 PM Jack <valac...@gmail.com> wrote:
Ok I'm very stuck.  Thought very hard about it but can't see how I can add a hidden field into the HTML, and relay the specified instance of the hidden field back to the views... or am I completely off about something?

Also in regards to the looped form's.  If you look at my original code, I actually do have a <form> representing a button for each agent.  The resulting page looks like this (excuse the bad CSS).  I'm not sure if that is what you meant by having a form for each agent.

So if I understood you correctly, I do have a separate <form> input for each displayed agent; looped with {% for agent in agent_list %}.  The question is, how do I relay the correct agent linked to its 'Send Invite' button back to the views?

--
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 django-users+unsubscribe@googlegroups.com.
--
--

Adam F. Simon, PhD
Calabasas, CA.

cell:      818-425-3719
home:   818-880-8405

Feel free to link w/ me: LinkedIn


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/k5mwo58aPfU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--

Jack Zhang

Adam Simon

unread,
Nov 9, 2017, 11:50:36 PM11/9/17
to django...@googlegroups.com

I would recommend taking a look at how Jquey does Ajax. It’s a lot simpler than Django forms. I will post some example code when I get home


On Thu, Nov 9, 2017 at 3:42 PM Jack Zhang <valac...@gmail.com> wrote:
No I am not familiar with Ajax.  I only know a bit of JavaScript.

On Thu, Nov 9, 2017 at 6:19 PM, Adam Simon <add.si...@gmail.com> wrote:

Hi Jack. I will write some more details when I get home. Have you ever used Ajax?
On Thu, Nov 9, 2017 at 3:14 PM Jack <valac...@gmail.com> wrote:
Ok I'm very stuck.  Thought very hard about it but can't see how I can add a hidden field into the HTML, and relay the specified instance of the hidden field back to the views... or am I completely off about something?

Also in regards to the looped form's.  If you look at my original code, I actually do have a <form> representing a button for each agent.  The resulting page looks like this (excuse the bad CSS).  I'm not sure if that is what you meant by having a form for each agent.

So if I understood you correctly, I do have a separate <form> input for each displayed agent; looped with {% for agent in agent_list %}.  The question is, how do I relay the correct agent linked to its 'Send Invite' button back to the views?

--
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 django-users...@googlegroups.com.
--
--

Adam F. Simon, PhD
Calabasas, CA.

cell:      818-425-3719
home:   818-880-8405

Feel free to link w/ me: LinkedIn


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/k5mwo58aPfU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
--
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 django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages