[Python] How to pass context to a different view function?

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024.

  1. Stack

    Stack Membro Participativo

    My models.py:

    class fields(models.Model):
    name = models.CharField(max_length=18)
    link = models.TextField()


    The link contains the hyperlink of the related name.

    My views.py:

    def index(request):
    listing = fields.objects.all()
    context ={'listing':'listing'}
    return render(request,'index.html',context)


    urls.py

    urlpatterns = [

    path('admin/', admin.site.urls),
    path('',index,name="index"),
    ]


    template:

    {% for i in listing %}
    <tr>
    <td data-label="name">{{ i.name }}</td>
    <td data-label="Quote"><button><a href ="{{ i.link}} " target="_blank">{{ i.link }}</a></button></td>
    <tr>
    {% endfor %}


    This redirects to the hyperlink that is displayed in the template but I want to do some automation after it redirects into this link.

    Now, I want to pass the context of this function which only includes the link to the other view function so that another view function will be:

    def bypass_link(request):
    # get the link from above function
    # execute some selenium scripts in the link


    The simple template to illustrate this will be:

    {% for i in listing %}
    <tr>
    <td data-label="name">{{ i.name }}</td>
    <td data-label="Quote"><button><a href ="{ % url 'bypass_link' %} " target="_blank">{{ i.link }}</a></button></td>
    <tr>
    {% endfor %}

    Continue reading...

Compartilhe esta Página