Django - How to add a link to a table that will open another page(django app) from an html template and pass a variable with it.

434 views
Skip to first unread message

Patrick Carra

unread,
Sep 25, 2019, 1:09:52 PM9/25/19
to Django users
Hello I have an app that displays some database information in a table.  Inside of the html template I am making an edit link that I want to open another app(page viewLit) while passing a value to it's view.  I have added my code below.  My question is I am unsure of how to make this links url and pass the object data located inside circuit.circuitid along with it.  I haven't been able to find the right way to code this yet and this is just how I thought that this should be done. If anyone has a better idea I am open to suggestions.

search_custom.html(code for link)
{% for circuit in filter.qs %}
<tr>
    <td class="actions">
        <a href="" class ="view-item" title ="View">View</a>
    </td>
    <td>{{ circuit.circuitid }}</td>
</tr>
{% endfor %}

myapp/myapp/urls.py
urlpatterns = [
    path('viewLit/', include('viewLit.urls')),
]

myapp/viewLit/urls.py
urlpatterns=[
    path('viewLit/circuitid.id', views.viewLit, name='viewLit'),
]

views.py
def viewLit(request, circuitid):
    #display records fields here
    return HttpResponse("You are at the viewLit page!")

Randy Hughes

unread,
Sep 25, 2019, 2:27:31 PM9/25/19
to django...@googlegroups.com
views.py
class viewLit(TemplateView):

def get(self, request, strategy=None):
circuit_id = self.request.query_params.get('circuitid', None)

urls.py

path('viewlit/', viewlit.as_view(), name='viewlit')


search_custom.html
<td>
<a href="{% url viewlit %}?circuitid={{ circuit.circuitid }}">
{{ circuit.circuitid }}
</a>
</td>


''' this is an example using a query parameter
the url in urls.py does not need to include this parameter as a slug

the view is able to get the parameter by key name (the same way you get a key from a dict)
the <a></a>tag is formatted to create the url ex (127.0.0.1/foo/) query parameters are everything following the ?
so as you can do something like this 127.0.0.1/foo/?field1=a&field2=b&field3=c
I am not sure if this is the better than your solution but this should work. As well you can add a redirect in viewlit to a different page if
circuit_id does not exist.
'''



--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d81bf60a-260c-44c1-9c12-e852b51432a9%40googlegroups.com.

Patrick Carra

unread,
Nov 7, 2019, 11:05:12 AM11/7/19
to Django users
Thank you Randy!

Patrick Carra

unread,
Nov 11, 2019, 5:35:27 PM11/11/19
to Django users
This worked for me but upon more testing I discovered some values such as WAVE/123456//IMP get changed to WAVE/123456//IMP during the process of passing this parameter as a url and django interpreting it.  As a result I get an error: Circuitinfotable matching query does not exist.  I'm assuming this is because django is interpreting the second / as an escape character?  How do I get it to include all /'s in the query so that a correct result will be returned.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages