I am using Geonode that is actually a django app installed inside a docker container. What I want to do is, to edit an html page of my app and add an extra button that when the user presses it a python script will run.
So far, I have added the button in the html page, a function in my views.py file and a url in the urls.py but it doesnt seem to work.
html page
{% trans "Approve Layer" %}
views.py
def layer_approval(request):
# I keep it simple to make sure it works
return redirect('www.google.gr')
urls.py
from django.urls import path
urlpatterns += [
url(r'^layer_approval/', include('geonode.views.layer_approval'))
]
I am completely new to Django. Any advice?
Your method name and URL-pattern layer_approval and pass button name or id layer_approve.
Related
I have used static for all the js/css/images path. However, click on the logo redirects me to http://127.0.0.1:8000/static/index.html without any css/js/images. The same works well all other paths in the page(eg: http://127.0.0.1:8000/static/about.html)
Sharing my urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, "index.html")
Please help
Screenshot here
Websites generally need to serve additional files such as images,
JavaScript, or CSS. In Django, we refer to these files as “static
files”.
NOTE: html files are not included, so don't use {% static %} with html files.
To jump to other pages, you have to use {% url %} tag with the name of the view, for example {% url 'index' %} or simply use a relative path Home, that's good to go.
For css/js files are not loading, you may have the wrong settings, follow the instructions here step by step, and remember to {% load static %} in the top of your template
When I run 'yarn start', my link to manifest.json in my index.html file works fine, but when I run 'python3 manage.py runserver' all I get in the terminal is:
Not Found: /manifest.json
"GET /manifest.json HTTP/1.1" 404 2234
This also happens to all of my static links and imports. I'm pretty new to Django and React, and programming as a whole, so I think that I'm just missing something simple, but I can't figure it out.
I've been trying to use {% load static %}, but the link doesn't work, even if I edit STATIC_URL in settings.py to point towards my manifest.json directory. I also attempted to edit view.py and urls.py, but all I get is syntax errors in the terminal. Other than that I'm clueless.
frontend/public/index.html
<html>
<head>
<title>WebProject</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<link rel="manifest" href="manifest.json"/>
</head>
<body style="background-color: #FAF0E6; font-family: Verdana; font-size: 40px;">
<div id="root"></div>
</body>
</html>
frontend/urls.py
from django.urls import path
from . import views
from django.conf.urls.static import static
urlpatterns = [
path('', views.index),
]
frontend/views.py
from django.shortcuts import render
def index(request):
return render(request, 'frontend/public/index.html')
I expected my browser to load manifest.json properly, along with any other links or imports, but I keep getting a blank page.
Im using React inside of Django, so when I tried to import my index.js the same "Not Found" terminal error popped up. Im assuming that if I solve the manifest.json problem, I'll also solve my other import and link problems.
I had the same issue and found this Can't get rid of missing manifest.json error
Well, in my case it was browser cache related and swapping to incognito mode was enough.
The same happened to me (blank page and unable to load manifest.json + react build's static files) and I solved the issues thanks to this excellent article
Solution -> assuming your react app (build etc.) are in a folder called frontend at the same level as your django project, in your settings.py file you need to make sure your STATICFILES_DIRS variable is set like below (don't forget the trailing coma as it is a tuple).
STATICFILES_DIRS = (
os.path.join(os.path.join(BASE_DIR, 'frontend'), 'build', 'static'),
)
In urls.py:
from django.urls import re_path
CHANGE:
urlpatterns = [
...
path('', TemplateView.as_view(template_name='index.html')]
TO:
urlpatterns = [
...
re_path('.*', TemplateView.as_view(template_name='index.html')]
Had the same error and worked for me.
I'm making Django app in which I need to embed many external HTML files in the template. Each HTML file is stored in its own directory, along with the subdirectory that contains all the images. The file structure:
Abstract1
Pictures
image1.png
image2.png
abstract1.html
I use a custom template tag for embedding (see below). My problem: the HTML files are loaded, but linked resources (e.g. img) are not working properly (i.e. they're not being displayed). HTML files use relative urls, which, mixed with the django template base path produce invalid url, but even if I use hardcoded absolute urls the problem remains. I feel like I'm missing something obvious. Is there some proper (or not proper but working) way to overcome such problem?
template
{% load abstracts_extras %}
<!DOCTYPE html>
<html>
<body style="margin-left:10px">
<h2>{{abstract}}</h2>
<b>Authors:</b><br/>
<ul>
{% for author in authors %}
<li>{{author}}</li>
{% endfor %}
</ul>
<p>
<b>Title: </b>{{abstract.title}}
<p>
<hr>
{% include_external filename|add:'.html' %}
</body>
</html>
abstracts_extras
from django.template import Library
register = Library()
def include_external (url):
url = 'file:///' + url
import urllib2
return urllib2.urlopen (url).read ()
If I am understanding well, your templates load but not statics like img.
It would be a configuration error.
You should check both settings.py for Django and httpd.conf for Apache and check staticfiles are properly configured.
Have you any error shown or just images are not loaded (but no error)?
I am having trouble to open a web page at the specific position from django view. After submitting a form, I wish it display where it was, but I cannot know where it has to be on the template itself, since that information is in form values.
Sounds like you need to define your success url as the same page from where the form was submitted, plus an anchor.
For example:
from os.path import join
class MyFormView(FormView):
template_name='some_form_template.html'
def get_success_url(self):
"""
Returns the supplied success URL.
"""
return join(reverse('viewname'), '#anchor')
In your some_form_template.html template:
<a id="anchor">Where you'd like the user to be dropped on the page.</a>
If your form validation was successful, the page will refresh and the user will be dropped to the anchor specified on that template.
I am trying to make dynamically generated download pages for some uploaded files on my site.
I've got routing set up and a template that will show the filename. I want to have a button on this page, that when pressed will call send_from_directory and download the file. How can I do this?
My function in python that renders the download page:
#app.route('/<new_folder_name>/', subdomain='f')
def uploaded_file(new_folder_name):
filename = subfolder_fetch(new_folder_name)
return render_template("download.html", filename=filename)
My button in html is just something like:
{% block body %}
<div id = "filename">
{{filename}}
</div>
<button name="dlbutton">Download</button>
{% endblock %}
I don't have a form or anything set up, do I need to do something like set one up and then catch the request with flask? How would I do this, or is there a simpler way?
Create a download_file route with your send_from_directory response and use a link to download your file:
Download