Django Reason for getting TemplateSyntaxError? - django

I have created a new app called newsletters. I can access the pages when I write their location directly from local host by writing
http://127.0.0.1:8000/newsletters/signup/
but when I try to add their url in the nav bar I am getting an error:
TemplateSyntaxError at /
Invalid block tag on line 40: 'url'newsletters:subscribe''. Did you forget to register or load this tag?
Here are the main project urls:
urlpatterns = [
path('admin/', admin.site.urls),
path('newsletters/', include('newsletters.urls', namespace='newsletters')),
]
Here are newsletters app urls:
app_name = 'newsletters'
urlpatterns = [
path('signup/', newsletter_signup, name="subscribe"),
path('unsubscribe/', newsletter_unsubscribe, name='unsubscribe'),
]
here are the nav bar template:
<div class="dropdown-divider"></div>
<a class="dropdown-item" href=" {% url'newsletters:subscribe' %}">Newsletters</a>
<div class="dropdown-divider"></div>
How should I fix it and what am I have I done wrong to avoid it?

You're missing a space after url in your url tag:
{% url 'newsletters:subscribe' %}

Related

media image is not able to display on webpage

when click on "yo" I am able to see media file but when tried to display image through the image tag it is not seen on webpage
yo
<img src="{{status.adv.url}}" width="250" height="250" alt="advertise"/>
Change this
<img src="{{status.adv.url}}" width="250" height="250" alt="advertise"/>
to
<img src="{% static status.adv.url %}" width="250" height="250" alt="advertise"/>
and include {% load static %} template tag on the top of your template(s)
Your project url.py should have these
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Documentation: Managing static files (e.g. images, JavaScript, CSS)
It was add blocker used by me. so disable your add blocker.
If you want to confirm that it is add blocker open your chrome and in console you will find this "Failed to load resource: net::ERR_BLOCKED_BY_CLIENT"

NoReverseMatch with slug variable, works with hard coded value

I'm updating some code from a Django 1.11 project to Django 2.x, and in one template (contacts list view with FK back to Clients), I am getting "NoReverseMatch" since it is not seeing the slug value in the URL parameter. If I hard code the value it works, and the same variable displays the proper value for each record if I just display as text on the page.
I don't see why the variable is getting dropped, but probably something stupid I've done.
This works:
<td class="">
<a href="{% url "clients:view" slug='sony' %}">
{{ contact.client.slug }}
</a>
</td>
This gets the NoReverse Error
<td class="">
<a href="{% url "clients:view" slug=contact.client.slug %}">
{{ contact.client.slug }}
</a>
</td>
In both cases, the variable {{ contact.client.slug }} returns the proper data.
urls.py:
import ...
app_name = "clients"
urlpatterns = [
path("ajax/validate_client_code", validate_client_code, name="validate_client_code"),
path("", ClientListView.as_view(), name="list"),
path("new/", ClientCreateView.as_view(), name="new"),
path("edit/<slug:slug>/", ClientUpdateView.as_view(), name="edit"),
path("delete/<int:pk>/", ClientDeleteView.as_view(), name="delete"),
path("<slug:slug>/", ClientDetailView.as_view(), name="view"),
]
Can anyone point out my mistake? Has to be something really simple, but I just cant see it.

How to get link to download file in pdf? (Django)

I have the following structure in the details.html
<p>Files:
{% for file in files %}
<p>{{ file.name }}</p>
{% endfor %}
</p>
This link returns the correct url.
http://localhost/media/file.pdf
However, every time I click, instead of downloading the file, it returns error 404.
What is the correct way to do this operation?
<p><a href="{{ file }}" download>{{ file.name }}</a></p>
if you try to query to on database and print in client side, so in your anchor link write a right path
example:
<p><a href="/media/{{ file }}" download>{{ file.name }}</a></p>
Is this on the local development server or in production?
My guess is you haven't configured Django to serve /media/ files.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
However, do note the admonition in the documentation:
This is not suitable for production use!

The a tag href is not my expectant url

I have a project(pyProject), and in it have a APP(app02).
And in the pyProject/urls.py, I routed the app02:
from app02 import views as app02_v
urlpatterns = [
url(r'^admin/', admin.site.urls),
...
url(r'^app02/', include("app02.urls")),
]
In the app02/urls.py:
import views
urlpatterns = [
url(r'^index/$', views.Index.as_view()),
...
url(r'^userinfo/', views.userinfo),
]
the app02/views.py:
def userinfo(request):
# QuerySet
user_list = models.UserInfo.objects.all()
print (user_list.query)
return render(request, 'app02/userinfo.html', {'user_list':user_list})
And in the templates:
templates/index.html:
<div style="position:absolute; top:48px; bottom:0; left:0; width:200px;background-color:#333">
<a class="menu" href="app02/userinfo/">用户管理</a>
<a class="menu" href="app02/ugroup/">用户组管理</a>
</div>
But when I click the <a class="menu" href="app02/userinfo/">用户管理</a> tag, print the error:
Not Found: /index/app02/userinfo/
Why it is not the /app02/userinfo/? why there is /index more ?
My expert is request the /app02/userinfo/.
Add a name keyword argument to your urls like this:
urlpatterns = [
url(r'^index/$', views.Index.as_view()), name='index',
...
url(r'^userinfo/', views.userinfo), name='user_info',
]
And then use the Django url template tag to render your URL's automatically like this:
<div style="position:absolute; top:48px; bottom:0; left:0; width:200px;background-color:#333">
<a class="menu" href="{% url 'user_info' %}">用户管理</a>
<a class="menu" href="app02/ugroup/">用户组管理</a>
</div>
The syntax is {% url '<view_name>' %} where view_name is the name defined in the url.
In the a tag href you should use the absolute path:
<a class="menu" href="/app02/userinfo/">用户管理</a>
If use the relative path, it will append after to your current url.

Django not rendering Glyphicons

Glyphicons are rendering as empty rectangles in my development server.
settings
from unipath import Path
BASE_DIR = Path(__file__).ancestor(2)
STATICFILES_DIRS = (BASE_DIR.child("static", "static_dirs"), )
STATIC_URL = '/static/'
template
{% load staticfiles %}
<span class="glyphicon glyphicon-heart"></span>
<span class="glyphicon glyphicon-calendar"></span>
bootstrap.css/bootstrap.min.css
src: url('../fonts/glyphicons-halflings-regular.eot');
src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/glyphicons-halflings-regular.woff') format('woff'),
url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'),
url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
urls
urlpatterns = patterns('',
url(regex = r'^$',
view = 'tesglyph.views.testglyph'),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When I add to
base.html
<head>
(...)
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">(...)
</head>
I get the heart, but not the calendar !
How do I fix this?
This seems excessively difficult, have you considered something like django-bootstrap3?
All needed would be-
{% load bootstrap3 %}
...
{% bootstrap_icon "heart" %}
Note that this solution means that you only code in the template, saving a ton of headache. Replace heart with the bootstrap icon name that you want to display. That's it.