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

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" %}'>

Related

Wagtail modeltranslation language switcher doesn't work on /search page

I have added search url to i18n_patterns, but the language switcher doesn't work on that page.
urls.py:
urlpatterns += i18n_patterns(
path("search/", search_views.search, name="search"),
path("", include(wagtail_urls)),
)
language switcher:
{% get_available_languages_wmt as languages %}
<div class="nav-item dropdown float-right">
<p class="nav-link dropdown-toggle m-auto" data-toggle="dropdown" role="button" aria-expanded="false">
{{ request.LANGUAGE_CODE|upper }}</p>
<div class="dropdown-menu w-25">
{% for language in languages %}
{% if language != request.LANGUAGE_CODE %}
<a class="dropdown-item" href="{% change_lang language page %}">{{ language|upper }}</a>
{% endif %}
{% endfor %}
</div>
</div>
Furthermore, when i add search url above root one the search page raises 404 page.
How can i make the language switcher work on the search page?
The language switcher uses the Wagtail page variable. See the change_lang template tag:
<a ... href="{% change_lang language page %}">...</a>
And search is a Django view, not a Wagtail page. The page variable is not defined.
You can make the switcher work on the search view by setting the href yourself:
{% for language in languages %}
<a href="/{{ language.code }}/search/"
{% endfor %}
Alternatively, you can create a Wagtail search page:
class SearchPage(Page):
def get_context(self, request):
context = super().get_context(request)
... # Copy the code from the current search view.
# Update and return the context
context.update({
'search_query': search_query,
'search_results': search_results,
})
return context
https://docs.wagtail.io/en/latest/topics/pages.html#customising-template-context
You also have to:
Rename (search_page.html) and rework (page.variable_name) your search template.
Remove the search url from urls.py
Add the SearchPage via the Wagtail admin interface to the page tree.

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

Django passing a string in url

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')

Django Link to class based views url

I created with django a page where I can delete my blog articles using the class based views, I want to add link to each article but I'm struggling to do it.
here is the code to link to the class based view
views.py
user_delete_article = Article.objects.filter(user=request.user)
template.html
{% for article in user_delete_article %}
<p><a class="readmore" href="{% url "article.views.DeleteView" article.id %}">{{ article.titre }}</a></p>
{% endfor %}
urls.py
url(r'^delete/(?P<id>\d+)/$', DeleteView.as_view(), name="DeleteView"),
How can I make this work?
Assuming this is django 1.8
<a href="{% url "DeleteView" id=article.id %}">

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">

Categories