In my view to get url parameters like this:
date=request.GET.get('date','')
In my url I am trying to pass parameters in this way with the url template tag like this:
<td><a href="{% url 'health:medication-record?date=01/01/2001' action='add' pk=entry.id %}" >Add To Log</a></td>
The parameter after the ? is obviously not working, how can I pass this data value in order to retrieve in with a get?
This question is related to
django
django-templates
Im not sure if im out of the subject, but i found solution for me; You have a class based view, and you want to have a get parameter as a template tag:
class MyView(DetailView):
model = MyModel
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['tag_name'] = self.request.GET.get('get_parameter_name', None)
return ctx
Then you make your get request /mysite/urlname?get_parameter_name='stuff
.
In your template, when you insert {{ tag_name }}
, you will have access to the get parameter value ('stuff'). If you have an url in your template that also needs this parameter, you can do
{% url 'my_url' %}?get_parameter_name={{ tag_name }}"
You will not have to modify your url configuration
This can be done in three simple steps:
1) Add item id with url
tag:
{% for item in post %}
<tr>
<th>{{ item.id }}</th>
<td>{{ item.title }}</td>
<td>{{ item.body }}</td>
<td>
<a href={% url 'edit' id=item.id %}>Edit</a>
<a href={% url 'delete' id=item.id %}>Delete</a>
</td>
</tr>
{% endfor %}
2) Add path to urls.py:
path('edit/<int:id>', views.edit, name='edit')
path('delete/<int:id>', views.delete, name='delete')
3) Use the id on views.py:
def delete(request, id):
obj = post.objects.get(id=id)
obj.delete()
return redirect('dashboard')
Simply add Templates URL:
<a href="{% url 'service_data' d.id %}">
...XYZ
</a>
Used in django 2.0
1: HTML
<tbody>
{% for ticket in tickets %}
<tr>
<td class="ticket_id">{{ticket.id}}</td>
<td class="ticket_eam">{{ticket.eam}}</td>
<td class="ticket_subject">{{ticket.subject}}</td>
<td>{{ticket.zone}}</td>
<td>{{ticket.plaza}}</td>
<td>{{ticket.lane}}</td>
<td>{{ticket.uptime}}</td>
<td>{{ticket.downtime}}</td>
<td><a href="{% url 'ticket_details' ticket_id=ticket.id %}"><button data-toggle="modal" data-target="#modaldemo3" class="value-modal"><i class="icon ion-edit"></a></i></button> <button><i class="fa fa-eye-slash"></i></button>
</tr>
{% endfor %}
</tbody>
The {% url 'ticket_details' %} is the function name in your views
2: Views.py
def ticket_details(request, ticket_id):
print(ticket_id)
return render(request, ticket.html)
ticket_id is the parameter you will get from the ticket_id=ticket.id
3: URL.py
urlpatterns = [
path('ticket_details/?P<int:ticket_id>/', views.ticket_details, name="ticket_details") ]
/?P - where ticket_id is the name of the group and pattern is some pattern to match.
First you need to prepare your url to accept the param in the regex: (urls.py)
url(r'^panel/person/(?P<person_id>[0-9]+)$', 'apps.panel.views.person_form', name='panel_person_form'),
So you use this in your template:
{% url 'panel_person_form' person_id=item.id %}
If you have more than one param, you can change your regex and modify the template using the following:
{% url 'panel_person_form' person_id=item.id group_id=3 %}
Source: Stackoverflow.com