224👍
No, because the GET parameters are not part of the URL.
Simply add them to the end:
<a href="{% url myview %}?office=foobar">
For Django 1.5+
<a href="{% url 'myview' %}?office=foobar">
47👍
A way to mix-up current parameters with new one:
{% url 'order_list' %}?office=foobar&{{ request.GET.urlencode }}
Modify your settings to have request variable:
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
'django.core.context_processors.request',
)
- [Django]-Get Timezone from City in Python/Django
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
- [Django]-How to check Django version
37👍
Use urlencode
if the argument is a variable
<a href="{% url 'myview' %}?office={{ some_var | urlencode }}">
or else special characters like spaces might break your URL.
Documentation: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#urlencode
- [Django]-How do you Serialize the User model in Django Rest Framework
- [Django]-Django custom field validator vs. clean
- [Django]-Where to put business logic in django
9👍
First, a silly answer:
{% url my-view-name %}?office=foobar
A serious anwser: No, you can’t. Django’s URL resolver matches only the path part of the URL, thus the {% url %}
tag can only reverse that part of URL.
- [Django]-Set up a scheduled job?
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Adding to the "constructor" of a django model
2👍
If your url (and the view) contains variable office
then you can pass it like this:
{% url 'some-url-name' foobar %}
or like this, if you have more than one parameter:
{% url 'some-url-name' office='foobar' %}
Documentation: https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#url
- [Django]-Django filter JSONField list of dicts
- [Django]-ImportError: Failed to import test module:
- [Django]-Adding django admin permissions in a migration: Permission matching query does not exist
1👍
Tried all the above, nothing worked.
My system has:
- Python 3.8.10
- python3 -m django –version 4.0.3
Here’s what worked for me:
urls.py
urlpatterns = [
path('', views.index, name='index'),
path('single/', views.single, name='single'),
]
views.py
def single(request):
url = request.GET.get('url')
return HttpResponse(url)
Calling from .html file
<a href={% url 'single' %}?url={{url}} target="_self">
Broswer URL
http://127.0.0.1:8000/newsblog/single/?url=https://www.cnn.com/2022/08/16/politics/liz-cheney-wyoming-alaska-primaries/index.html
- [Django]-Difference between User.objects.create_user() vs User.objects.create() vs User().save() in django
- [Django]-How to test Django's UpdateView?
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
0👍
Try this:
{% url 'myview' office=foobar %}
It worked for me. It basically does a reverse on that link and applies the given arguments.
- [Django]-How do I get the class of a object within a Django template?
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)