Django passing a string in url - python

I want to pass a string, in the url, and then catch in in the view
<a href="{% url 'dolgozo:dolgozo-detail' orderby=nev %} ">
I want to pass "nev" string.
url(r'^orderby=(?P<nev>.+)$', login_required(DolgozokListView.as_view(template_name="DolgozoKarbantart/DolgozokList.html")), name='dolgozo-detail'),
What is the regex for this, and how can i catch it in the view?

Why not try the simple one...
HTML
<a href="{% url 'dolgozo:dolgozo-detail' %}/?orderby={{ nev }}">
URL
url(r'^orderby/$', login_required(DolgozokListView.as_view(template_name="DolgozoKarbantart/DolgozokList.html")), name='dolgozo-detail'),
And in the view simply get the orderby using GET
orderby = request.GET.get('orderby')

html, no need for / before ?
<a href="{% url 'dolgozo:dolgozo-detail' %}?orderby={{ nev }}">
urls.py
url(r'^orderby/$', login_required(DolgozokListView.as_view(template_name="DolgozoKarbantart/DolgozokList.html")), name='dolgozo-detail'),
views.py
orderby = request.GET.get('orderby')

Related

Python/ Django- copying buttons from one HTML page to another

I have just taken over the development of some project management software that has been written in Python/ Django- having not used Python or Django much at all before...
There are a few buttons displayed on one of the webpages that it would be useful to display on another page within the application. I can see that these buttons are defined in budget.html with the following code:
{% block page_options %}
<a class="button m-r-md" href="{% url 'costing:export_csv' budget.id %}">Export to Excel</a>
<a class="button m-r-md" href="{% url 'costing:schedule_of_works_post_dep' budget.id %}" target="_blank">Schedule of works</a>
<a class="button m-r-md" href="?pdf=1" target="_blank">PDF</a>
<input data-view-url="{% url 'costing:combined_budget' project.id %}?search=" type="text" id="item_search" placeholder="Item search" />
{% endblock page_options %}
The other page, where I want to be able to use them- variations.html has the following code already in its {%block page_options %} block:
{% block page_options %}
<button class="button modalBtn" name="variation">+ Add variation</button>
<a class="button" href="{% url 'costing:add_omit_builder' project.id %}">+ Add group</a>
<a class="button" id="scas" data-view-url="{% url 'costing:preview_scas' project.budget_overview.version.id %}" href="{% url 'costing:scas_2_variations' project.budget_overview.version.id %}">+ Standard cost assumptions</a>
<!--ERF(17/11/2016 # 1700) Add buttons to export adds/ omits table to excel -->
{% endblock page_options %}
So I tried copying and pasting the code from the first page into this block in the second page:
{% block page_options %}
<button class="button modalBtn" name="variation">+ Add variation</button>
<a class="button" href="{% url 'costing:add_omit_builder' project.id %}">+ Add group</a>
<a class="button" id="scas" data-view-url="{% url 'costing:preview_scas' project.budget_overview.version.id %}" href="{% url 'costing:scas_2_variations' project.budget_overview.version.id %}">+ Standard cost assumptions</a>
<!--ERF(17/11/2016 # 1700) Add buttons to export adds/ omits table to excel -->
<a class="button m-r-md" href="{% url 'costing:export_csv' budget.id %}">Export to Excel</a>
<a class="button m-r-md" href="{% url 'costing:schedule_of_works_post_dep' budget.id %}" target="_blank">Schedule of works</a>
<a class="button m-r-md" href="?pdf=1" target="_blank">PDF</a>
<input data-view-url="{% url 'costing:combined_budget' project.id %}?search=" type="text" id="item_search" placeholder="Item search" />
{% endblock page_options %}
but when I now try viewing this page in the browser, I get an error page which says:
NoReverseMatch at /costing/5915/variations/
I'm not sure why I'm getting this error... do I need to reference any of the calls to the views that I'm using in the code I've copied in elsewhere in the HTML file? Both of the HTML files are in the same app, so share the same models.py file- so I would have thought that they would both be able to use all of the models & views defined within this app?
Is this the case? If so, why am I getting this error on the variations page?
Based on what you said I suppose problem is that your don't have "budget" value in context variable. I think that view which using first template send "budget" in it's context.
But second view don't. That is why when you try to get budget.id in the second template you get the error.
Try to modify context in your view and add "budget" variable to it.
def my_view(request, pk):
budget= get_object_or_404(Budget, pk=pk)
return render(request, 'budget.html', {'budget': budget})
Or if you are using class based view you should override get_context_data method:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['budget'] = self.budget
return context

How to call a "wizard view" with {% url %} statement in Django?

urls.py:
url(r'^signin_client$', views.signin_client, name='signin_client') # normal view,
url(r'^signup_owner$', SignupOwnerWizard.as_view()), # wizard view
In a 'normal view' case: <a href='{% url "myapp.views.signin_client" %}'> works.
But I can't figure how to do the same thing for my "wizard view". Of course, I don't want to hardcode the url.
Add name to the pattern:
url(r'^signup_owner$', SignupOwnerWizard.as_view(), name='signup_owner'),
And than use the name in {% url %} tag:
<a href='{% url "signup_owner" %}'>
If you use namespaces you would need a namespace prefix:
<a href='{% url "mynamespace:signup_owner" %}'>

Opening external Url in Django template

I've a Django template like this:
<ul>
{% for url in urls %}
<li>{{ url.url_title }}</li>
{% endfor %}
</ul>
Url is a model that stores Url name and Url title of that particular url. I thought by using this template, I might be able to open the page and get redirected to the external url specified in:
<a href="{{ url.url_name }}">
Turns out, I can't. How do I achieve this? I'm a newbie in Django and don't know what to do.
I supposed url_name is Charfield type, and it is something like example.com or starting with http or https, for all above cases, the below worked for me:
<a href="http://{{ url.url_name }}">
If you used URL type for the url_name, then your solution should work and your problem is not with tag.
Please, try with:
<a href="{{ url.url_name }}" target="_self">

Django-like no reverse for url

I am using this plugin
There was an error with the url so I changed it from
<a class="liker" href="{% url like content_type content_obj.id 1 %}" rel="nofollow">I Like</a>`
to
<a class="liker" href="{% url 'like' content_type content_obj.id 1 %}" rel="nofollow">I Like</a>
as recommended in this fix but I am still getting this error
Reverse for 'like' with arguments '(u'snippets-snippets', None, 1)' and keyword arguments '{}' not found.
EDIT:
This is the urls.py from the app
urlpatterns = patterns(
'likes.views',
url(r'^like/(?P<content_type>[\w-]+)/(?P<id>\d+)/(?P<vote>-?\d+)$', 'like',
name='like'),
)
My urls.py simply includes it
urlpatterns = patterns('snippets.views',
(r'^likes/', include('likes.urls')),
)
Looking at your error, it seems like your content_obj.id is evaluating to None.
You might want to see if that object indeed exists. If not, you might have to do a sanity check. Something like
{% if content_obj.id %}
<a class="liker" href="{% url 'like' content_type content_obj.id 1 %}" rel="nofollow">I Like</a>
{% endif %}
Or pass the content_obj in the context appropriately.

give an active to class to active link

I am writing a python website built on the back of the django framework, I am looking for a way to highlight the current link the user is on depening on what the URL, I thought doing some thing like this would work.
What I have done is create a new application called nav and built some templatetags, like so,
from django import template
register = template.Library()
URL_PATTERNS = {
'home': (r'^/$',),
}
#register.tag
def nav_selection(parser, token):
try:
tag_name, nav_item = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
if not (nav_item[0] == nav_item[-1] and nav_item[0] in ('"', "'")):
raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
return NavSelectionNode(nav_item[1:-1])
class NavSelectionNode(template.Node):
def __init__(self, nav_item):
self.nav_item = nav_item
def render(self, context):
if not 'request' in context:
return ""
import re
try:
regs = URL_PATTERNS[self.nav_item]
except KeyError:
return ''
for reg in regs:
if re.match(reg, context['request'].get_full_path()):
return "active"
return ''
In my template I do this
<ul id="navigation">{% load nav %}
<li><a href="{% url views.home %}" class='{% nav_selection "home" %}'>home</a></li>
<li><a href="{% url views.about %}" class='{% nav_selection "about" %}'>about neal & wolf</a></li>
<li><a href="{% url shop.views.home %}" class='{% nav_selection "shop" %}'>our products</a></li>
<li><a href="{% url shop.views.home %}" class='{% nav_selection "shop" %}'>shop</a></li>
<li><a href="{% url views.look %}" class='{% nav_selection "look" %}'>get the look</a></li>
<li><a href="{% url news.views.index %}" class='{% nav_selection "news" %}'>news</a></li>
<li><a href="{% url contact.views.contact %}" class='{% nav_selection "contact" %}'>contact us</a></li>
<li><a href="{% url store_locator.views.index %}" class='{% nav_selection "finder" %}'>salon finder</a></li>
<li><a href="{% url professional.views.index %}" class='{% nav_selection "contact" %}'>neal & wolf professional</a></li>
</ul>
yet the markup I get out in firebug is this in this example I am browsing the index page
<a class="" href="/home/">
So something is obviously failing but I cannot see where, can anyone help me please?
Some things to check:
Is the request object actually in your context? Are you passing it in specifically, or are you using a RequestContext?
Why are you defining regexes in your templatetags, rather than using the built-in reverse function to look them up in the urlconf?
Do the regexes here actually match the ones in the urlconf?
Have you included your home urlconf under the 'home' url somehow?

Categories