Django - Send id to another template?

1

Template Detail of the tournament with its teams, when adding equipment, send the template to create a team, such as sending the tournament id and linking the team directly, without the need for a selection.

models.py

class Equipo(models.Model):
    nombre = models.CharField(max_length=200)
    color = RGBColorField()
    torneo = models.ForeignKey(Torneo, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return (self.nombre)

views.py

class Equipo_CreateView(CreateView):
    model = Equipo
    template_name = "torneos/equipo_crear.html"
    form_class = Equipo_Form
    success_url = reverse_lazy('torneos:equipo_crear')

    def get_context_data(self, **kwargs):
        torneo = Torneo.objects.get(id=kwargs['pk'])

    def form_valid(self, form_class):
        form_class.instance.user_id = self.request.user.id
        return super(Equipo_CreateView, self).form_valid(form_class)

urls.py

    url(r'^detalle/(?P<pk>\d+)/$', login_required(Torneo_DetailView.as_view()), name='torneo_detalle'),
    url(r'^(?P<pk>\d+)/equipo/$', login_required(Equipo_CreateView.as_view()), name='equipo_crear'),

tournament_detail.html

    <a href="{% url 'torneos:equipo_crear' object.id%}">Agregar Equipo</a>
    
asked by jhon perez 11.04.2017 в 21:47
source

1 answer

1

To pass data from the template to the view you have several options.

Via URL

/detalle_torneo/5

That would be managed via url and in the template you have to collect it as a paramenter:

url(r'^detalle_torneo/(?P<torneo_id>\d+)/$', 'nombreview', name='nombreurl')
def nombreview(request, torneo_id):
    torneo = Torneo.objects.get(id=torneo_id)
    #El resto de cosas pasandole a la view el torneo directamene

If it were a Class based view:

class nombreview(TemplateView):
    def get_context_data(self, **kwargs):
        torneo = Torneo.objects.get(id=kwargs['torneo_id'])

Send a GET parameter

a GET parameter would be generated by adding the name of the variable + the value after the URL url

<a href="#">#                                    
answered by 12.04.2017 в 17:26