I've been working on this for a few hours and for the life me of I can't get a variable from my django app into a template.
html:
{% block content %}
{{test}}
{% endblock content %}
views:
def home(request):
context = {'test' : "test"}
return render(request, "/dbus/templates/index.html", context)
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = 'home')
]
(note: I left the imports out of the view code sample I provided)
I think you dont need to add 'templates' in the argument "/dbus/templates/index.html"
If dbus is your django-app and templates is a folder inside this django-app, then by convention, you've to add a subfolder named dbus inside this templates folder.
All HTML files regarding the django-app 'dbus' should be stored inside this subfolder named 'dbus' (which comes inside templates folder)
The path of index.html should looks like: YourProjectFolder/dbus/templates/dbus/index.html
If you're right till this part, then you've to render the html page like "dbus/index.html"
You dont need to add that templates directory in the render argument.
So correct line is: return render(request, "dbus/index.html", context)
This should solve your problem!
Turn's out the issues was due to caching in either Nginx or uWSGI. I restarted both and the changes started working
Related
I'm learning Django and I have a problem that there is no answer for it on internet
here is the problem,
I created a folder as 'app', and I create another folder in app as 'challenges' (in VS Code in a Django application),
so we have 2 nested folders
now I want to create an HTML page, and I create a file as 'index.html',
but VS Code cannot know this file as an HTML file
You need to make a folder structure like this
>app_name
>>...
>>templates
>>>app_name
>>>>index.html
views.py
from django.shortcuts import render
def index(request):
return render(request, 'app_name/index.html)
urls.py
from . import views
urlpatterns = [
path('', views.index),
]
I have a project called 'my_project' and within that project I have an app called 'my_app' so I have two urls.py files. All of my url's for my_app are located within it's urls.py file and work correctly, except one. That one is 'download_file'. My site works when this is included in my_project's urls.py, but when it's in my_app's urls.py I get a NoReverseMatch error on page load.
I don't know why this url only works when it's located in my main projects url's folder. I suspect it has something to do with the regex, though I can't figure it out.
The user would be on this page:
http://127.0.0.1:8000/user_area/username/classes
then click the 'download' link:
<a href="{% url 'download_file' file_path=item.instance.user_file %}" target='_blank'>{{ item.instance.filename }}</a>
my_project.py
urlpatterns = [
# reference to my_app
re_path(r'^user_area/(?P<username>[\w-]+)/', include('my_app.urls')),
]
# this works
url(r'^download_file/(?P<file_path>(.+)\/([^/]+))$', users_views.DownloadFile.as_view(), name='download_file'),
]
my_app.py
urlpatterns = [
path('classes', views.classes, name='classes'),
# if I remove the url from my_project.py this one returns NoReverseMatch on page load
url(r'^download_file/(?P<file_path>(.+)\/([^/]+))$', users_views.DownloadFile.as_view(), name='download_file'),
Thank you.
The problem is occurring because your URL template tag is providing only one parameter: file_path.
This works when the URL is declared in your project urls.py, because only one parameter is needed.
When you try to use the URL in my_app.urls, you need to also provide the username parameter. You will need to use something like:
<a href="{% url 'download_file' username=request.user.username file_path=item.instance.user_file %}" target='_blank'>{{ item.instance.filename }}</a>
I'm trying to migrate my application from Django 1.0 to 1.6 and am running into a problem rendering templates with URL redirections.
My application structure is:
mysite
settings.py
urls.py
myapp
urls.py
views.py
etc.
myapp.urls contains:
from mysite.myapp import views
urlpatterns = patterns('',
# Login/Logout/Registration
url(r'^login/$', views.login, name="login"),
url(r'^registration/$', views.registration, name="registration"),
)
When I browse to /login, the URL is mapped correctly and invokes the view. However, when the view attempts to render its template, it generates this error:
ImportError at /login
No module named myapp
and points to this redirection syntax in the template:
<a href="{% url 'registration' %}">
I can browse to /registration without errors, so it has something to do with resolving back to the URL.
It must be something simple, but I'm stumped. Any suggestions would be greatly appreciated.
Finally tracked down the problem. What I had coded was correct. However, there were some other url patterns at the bottom of my url.py file that I had not converted to 1.6. For example
url(r'/schedule/$', 'schedule_list'),
And when the URL resolver looked for a reverse match, it apparently looped through the url patterns starting at the bottom and errored immediately. I guess I assumed it would either start at the top of the url patterns or there was some sort of lookup table that mapped names back to urls.
Project layout was changed in django 1.4.
Delete mysite part of the module name in myapp/urls.py. It should be:
from myapp import views
I have a bunch of html files. I'd like to render them in my Django View without converting them to Django templates. Is this possible?
Or is there any code to do the conversion easily?
Actually, you don't have to write a view for such cases. You can use direct_to_template shortcut in urls.py as is.
From generic views doc
from django.conf.urls import patterns, url, include
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
('^about/$', direct_to_template, {
'template': 'about.html'
}),
)
It is possible, just use the following method call:
def main_page(request):
template=get_template('index.html')
variables=Context({})
output = template.render(variables)
return HttpResponse(output)
Here your index.html file is located in templates folder.
Django templates are built on top of plain old files. If you have HTML files that don't have {{ or {% in them, then they are automatically valid Templates, albeit consisting of a single Text node.
Just reference them as you would reference any template, and it will "just work".
return render(request, "my-plain-html-file.html")
I stumbled on a silly situation with Django's RequestContext thing. Here is my question:
I stored all my images in my media/uploads file. In my template I'm simply using :
{% for photo in photos %}
<img src="{{gallery_root}}/{{photo.get_name}}" />
{% endfor %}
My view is :
def gallery_view(request):
photos = Photo.objects.all()
return render_to_response('gallery/sampleGallery.html',{'photos':photos},context_instance=RequestContext(request))
In my settings file :
GALLERY_ROOT = os.path.join(MEDIA_ROOT, "media/uploads")
And i have a contextprocessor file which contains:
from django.conf import settings
def gallery_root(request):
return {'gallery_root':settings.GALLERY_ROOT}
When I open my template, the image's path appears however the server gives 404, the path seems correct but django can not serves them.. So what's the reason that I can not see images on my template ?
The image source appears like this :
<img src="/Users/imperium/Desktop/sample/media/uploads/popo.jpg" />
Hey there, it's probably that your media isn't being served propery.
Try something like this in your urls.py file.
# if we're in DEBUG mode, allow django to serve media
# This is considered inefficient and isn't secure.
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.GALLERY_ROOT}),
)
MEDIA_ROOT is the filesystem path to your media, not the URL path. Building on the suggestion from mongoose_za, your template should look like:
{% for photo in photos %}
<img src="/media/{{photo.get_name}}" />
{% endfor %}
Of course, you can define a new constant in settings.py which corresponds to the URL root you've chosen, and use this both in urls.py as well as in your templates.