156π
I found the answer here: Is it possible to pass query parameters via Django's {% url %} template tag?
Simply add them to the end:
<a href="{% url myview %}?office=foobar">
For Django 1.5+
<a href="{% url 'myview' %}?office=foobar">
[there is nothing else to improve but Iβm getting a stupid error when I fix the code ticks]
246π
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 %}
- [Django]-How to access the local Django webserver from outside world
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-How to use regex in django query
41π
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')
- [Django]-How do I create a slug in Django?
- [Django]-Celery. Decrease number of processes
- [Django]-Disable a method in a ViewSet, django-rest-framework
39π
Simply add Templates URL:
<a href="{% url 'service_data' d.id %}">
...XYZ
</a>
Used in django 2.0
- [Django]-Stack trace from manage.py runserver not appearing
- [Django]-Dynamic choices field in Django Models
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
15π
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
- [Django]-How do I raise a Response Forbidden in django
- [Django]-What's the best solution for OpenID with Django?
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)
9π
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.
- [Django]-Setting Django up to use MySQL
- [Django]-How to define two fields "unique" as couple
- [Django]-Django β Website Home Page