python 2.7 DJANGO 1.11.14 win7
when I click the link in FWinstance_list_applied_user.html it was supposed to jump to FW_detail.html but nothing happened
url.py
urlpatterns += [
url(r'^myFWs/', views.LoanedFWsByUserListView.as_view(), name='my-applied'),
url(r'^myFWs/(?P<pk>[0-9]+)$', views.FWDetailView.as_view(), name='FW-detail'),
views.py:
class FWDetailView(LoginRequiredMixin,generic.ListView):
model = FW
template_name = 'FW_detail.html'
models.py
class FW(models.Model):
ODM_name = models.CharField(max_length=20)
project_name = models.CharField(max_length=20)
FW_detail.html
{% block content %}
<h1>FW request information: {{ FW.ODM_name}};{{ FW.project_name}}</h1>
<p><strong>please download using this link:</strong> {{ FW.download }}</p>
{% endblock %}
FWinstance_list_applied_user.html
{% block content %}
<h1>Applied FWs</h1>
{% if FW_list %}
<ul>
{% for FWinst in FW_list %}
{% if FWinst.is_approved %}
<li class="{% if FWinst.is_approved %}text-danger{% endif %}">-->
{{FWinst.ODM_name}} ({{ FWinst.project_name }})
</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>Nothing.</p>
{% endif %}
{% endblock %}
the image of FWinstance_list_applied_user.html, when I click the link CSR, nothing happened
You haven't terminated your "my-applied" URL pattern, so it matches everything beginning with "myFWs/" - including things that that would match the detail URL. Make sure you always use a terminating $ with regex URLs.
url(r'^myFWs/$', views.LoanedFWsByUserListView.as_view(), name='my-applied'),
You are seeing this behavior because your first URL pattern is not terminated. The regular expression r'^myFWs/' also matches the URL path myFWs/123, so your FW-detail URL is never matched. You can fix that by simply appending a $ to your URL pattern.
urlpatterns += [
url(r'^myFWs/$', views.LoanedFWsByUserListView.as_view(), name='my-applied'),
# ^ this was missing
Related
I try to pass information to an html template from a view function. Every time I try to call the variable from the html template it doesn't show anything.
Here is my configure_peplink.html:
{% extends "base_generic.html" %}
{% block content %}
<h1>Configure Peplink</h1>
<p>Configure a Peplink router from the web. This was designed by <em>Valorence LLC</em></p>
{% if peplink %}
<p>Serial Number: {{ peplink.serial_number }}</p>
<p>IP Address: {{ peplink.ip_address }}</p>
<p>Mac Address: {{ peplink.mac_address }}</p>
<p>Name: {{ peplink.name }}</p>
{% else %}
<p>No Data Found Off Device</p>
{% endif %}
{% endblock %}
Here is the view function configure_peplink:
def configure_peplink(request, peplink):
selected_peplink = PeplinkDevice.objects.get(serial_number=peplink)
print(selected_peplink.ip_address)
print(selected_peplink.serial_number)
print(selected_peplink.mac_address)
context = {
'peplink': selected_peplink
}
return render(request, 'configure_peplink.html', context=context)
Here is the url line to call the view:
re_path(r'^configurepeplink/(?P<peplink>.*)/$', views.configure_peplink, name='configurepeplink')
I've tested to make sure that the context has data in it (as seen with the print statements). Even though the context variable has data and is getting past the if statement in the html template it still doesn't display any data. I have tried clearing my cache on the browser and restarting all my services (django, celery, redis-server).
Here is a picture of the webpage:
The peplink variable (which is being used by the regex url and the view function) seems to be causing the problem. Change the name of the key or change the regex url variable for this to work. To get this to work by changing the key name in the view function do the following in the view function:
def configure_peplink(request, peplink):
selected_peplink = PeplinkDevice.objects.get(serial_number=peplink)
print(selected_peplink.ip_address)
print(selected_peplink.serial_number)
print(selected_peplink.mac_address)
context = {
'selected_peplink': selected_peplink
}
return render(request, 'configure_peplink.html', context=context)
Then change the html template to the following:
{% extends "base_generic.html" %}
{% block content %}
<h1>Configure Peplink</h1>
<p>Configure a Peplink router from the web. This was designed by <em>Valorence LLC</em></p>
{% if selected_peplink %}
<p>Serial Number: {{ selected_peplink.serial_number }}</p>
<p>IP Address: {{ selected_peplink.ip_address }}</p>
<p>Mac Address: {{ selected_peplink.mac_address }}</p>
<p>Name: {{ selected_peplink.name }}</p>
{% else %}
<p>No Data Found Off Device</p>
{% endif %}
{% endblock %}
I am working on a small web app to view some logfiles. But the
queries I issue to the database use to get very big.
I wanted to implement some pagination following this example pagination.
I put the class into a single file which gets loaded in the Flask view. Next I implemented
my pagination view like this:
#app.route('/index/', defaults={'page':1})
#app.route('/index/page/<int:page>')
def index(page):
count = db_session.execute("select host,facility,level,msg from messages").rowcount
tblqry = db_session.execute("select host,facility,level,msg from messages").fetchmany(size=1000)
if not tblqry and page != 1:
abort(404)
pagination = Pagination(page, PER_PAGE, count)
return render_template('index.html', pagination=pagination, tblqry=tblqry)
after that I created a macro file named _pagination_helper.html with the macro contents from the macro example. Then I imported the pagination_helper macro with:
{% from "_pagination_helper.html" import render_pagination %}
but when I then try to do something like this:
{{ render_pagination(host[0]) }}
flask claims:
UndefinedError: 'str object' has no attribute 'iter_pages'
so why does flask fails to find the 'iter_pages' because I included the pagination class in the views file?
And also I am not really sure where to put the URL Generation Helper from the How To.
Edit:
This is what my pagination_helper looks like:
{% macro render_pagination(pagination) %}
<div class=pagination>
{% for page in pagination.iter_pages() %}
{% if page %}
{% if page != pagination.page %}
{{ page }}
{% else %}
<strong>{{ page }}</strong>
{% endif %}
{% else %}
<span class=ellipsis>…</span>
{% endif %}
{%- endfor %}
{% if pagination.has_next %}
Next »
{% endif %}
</div>
{% endmacro %}
You are expected to pass a Pagination object to the macro, not a string. host[0] is a string, not the pagination value you created in your view function.
Use:
{{ render_pagination(pagination) }}
I am trying to highlight a link of a currently opened category tab, here is what i have already done:
globs.py
def globs(request):
cats = Category.objects.all()
return {'cats': cats}
views.py
def news_by_category(request, slug):
c = Category.objects.get(slug=slug)
news = News.objects.filter(category=c, status='p').order_by('-id')
#news = c.news_set.all().order_by('-id')
return object_list(
request,
news,
paginate_by = 5,
extra_context = {'c':c},
template_name = 'news_by_category.html')
base.html #bodyclass
<body class="{% block bodyclass %}{% endblock %}">
news_by_category.html
{% block bodyclass %}{{c|cut:" "}}{% endblock %}
base.html
<li><h4>Categories:</h4></li>
{% for i in cats %}
<li class="{{i.name|safe|cut:" "}}_li">
{{ i.name }}
</li> {% endfor %}
What i need to do now is to create style for every category, in category list, I could achieve this easily by styling inside a html file, but I'm not sure wether that would be proper (Would it?). I came up with some css styling,
{% for i in cats %}
body.{{ i|safe|cut:" "}} li.{{i|safe|cut:" "}}_li {
color: red;
}
but as I can't use django template tags inside my .css file, this wont work.
My questions:
1) How could i make this css file work for me. Any chance for a little step by step?
2) If I failed step1, how improper would it be to style those few li elements inside html file?
EDIT: /trying another way
I tried using:
base.html
{% for i in cats %}
<li class="{% ifequal 'request.get_full_path' '/k/{{ i.slug }}/' %}active{% endifequal %}">
{{ i.name }}
</li> {% endfor %}
.css
.active {{color:red;}
When i compared {{ request.get_full_path }} and /k/{{i.slug}}/ both returned same thing... but if its inside ifequal it doesnt seem to work.
You can create a simple class named "active" or something along those lines and add it to the current tab. Then, in your CSS you apply the active styles to that class. So you just append the active class and it'll automatically take the active style.
If you have a url:
{% url app:home i.slug as home %}
<li {% ifequal request.get_full_path home %}class="active"{% endifequal %}>
Please help me. When I trying to access http://127.0.0.1:8000/myblog/ It display empty object list.
Here is my template file.
{% extends "base_entries.html" %}
{% block title %}{{block.super}} | Latest entries{% endblock %}
{% block content %}
{% for entry in object_list %}
<h2>{{entry.title}}</h2>
<p>Published on {{entry.pub_date|date:"F j, Y"}}</p>
{% if entry.excerpt_html %}
{{entry.excerpt|safe}}
{% else %}
{{entry.body_html|truncatewords_html:"50"|safe}}
{% endif %}
<p>Read Full entry</p>
{% endfor %}
{% endblock %}
this is my entries.py url. I have included into main urls.py
`from django.conf.urls.defaults import *
from myblog.models import Entry
entry_info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^$',
'archive_index',entry_info_dict,
'blog_entry_archive_index'),
)`
In archive_index generic view, the default name of the template variable is latest, not object_list.
archive_index documentation
I have two similar codes. The first one works as expected.
urlpatterns = patterns('',
(r'^(?P<n1>\d)/test/', test),
(r'', test2),
{% url testapp.views.test n1=5 %}
But adding the second parameter makes the result return empty string.
urlpatterns = patterns('',
(r'^(?P<n1>\d)/test(?P<n2>\d)/', test),
(r'', test2),)
{% url testapp.views.test n1=5, n2=2 %}
Views signature:
def test(request, n1, n2=1):
Try
{% url testapp.views.test n1=5,n2=2 %}
without the space between the arguments
Update:
As of Django 1.9 (and maybe earlier) the correct way is to omit the comma and separate arguments using spaces:
{% url testapp.views.test n1=5 n2=2 %}
Here's an actual example of me using this technique. Maybe this will help:
{% if stories %}
<h2>Stories by #{{author.username}}</h2>
<ul>
{% for story in stories %}
<li>{{story.title}}</li>
{% endfor %}
</ul>
{% else %}
<p>#{{author.username}} hasn't published any stories yet.</p>
{% endif %}
https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url
From Django 1.5 Warning Don’t forget to put quotes around the function path or pattern name!
{% url 'some-url-name' arg1=v1 arg2=v2 %}