Upload image via Django admin - python

At the moment I am working on a Django project for a scholarship using Django 1.6.4. I would like to upload some images to my apache server. Unfortunately, I was not able to overcome the "Permission denied" message after filling out the form in the admin page. My project is located under /home/nick/Workspace/Web/kleyboldt_django/. There is my virtualenv directory and my real project called kleyboldt_homepage located (kleyboldt is the name of the guy I am writing for). My model looks like this:
/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage/models.py:
class stipendiat(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
content = models.TextField()
year = models.IntegerField()
image = models.ImageField(upload_to='stipendiaten/', default='default.jpg')
My settings are:
/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/kleyboldt_homepage/settings.ps:
...
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static-only')
STATIC_URL = '/static/'
...
and
/etc/apache2/apache2.conf:
WSGIDaemonProcess mks.com python-path=/home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage:/home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages
WSGIProcessGroup mks.com
<VirtualHost *:80>
Alias /robots.txt /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/robots.txt
Alias /favicon.ico /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only/favicon.ico
<LocationMatch "\.(jpg|css|gif|pdf|ico)$">
SetHandler None
</LocationMatch>
Alias /media/ /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media/
Alias /static/ /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only/
Alias /static/admin /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only/admin/
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/media>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/static/static-only>
Require all granted
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /home/nick/Workspace/Web/kleyboldt_django/kleyboldt.wsgi
ServerName mks.com
<Directory /home/nick/Workspace/Web/kleyboldt_django/>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Directory /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/>
Require all granted
Order allow,deny
Allow from all
</Directory>
<Files /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/homepage.log>
Require all granted
Order allow,deny
Allow from all
</Files>
<Directory /home/nick/Workspace/Web/kleyboldt_django/env/lib/python2.7/site-packages/>
Require all granted
Order allow,deny
Allow from all
</Directory>
ErrorLog /home/nick/Workspace/Web/kleyboldt_django/kleyboldt_homepage/apache_error.log
LogLevel debug
</VirtualHost>
Sorry that I wrote so much but I wanted to be sure that I did not forget a directory.
mks.com is redirected to my local machine with an entry in /etc/hosts.
The permissions for my media directory are:
drwxrwxrwx 4 nick www 4096 Mai 20 13:21 media
Does anybody know how I can solve this error?

Related

Static files are not loading after deployment on digitelocean

I have deploye my django application on digitel ocean by following this
blog:alocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04
this is the url as an error i am getting in console for static files
(index):5913 GET http://67.205.160.21/static/js/dashkit.min.js
net::ERR_ABORTED 403 (Forbidden) (index):5913 GET
http://67.205.160.21/static/js/style.js net::ERR_ABORTED 403
(Forbidden)
every thing is fine except static file,
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
after running the python3 manage.py collect static it gives this
/home/podstatsclub/webapp/Podcast_stats/static
which i have put into default.config file which look something like this
Alias /static /home/podstatsclub/webapp/Podcast_stats/static
<Directory /home/podstatsclub/webapp/Podcast_stats/static>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/podstatsclub/webapp/Podcast_stats/podcast>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess Podcast_stats python-home=/home/podstatsclub/webapp/Podcast_stats/env pytho>
WSGIProcessGroup Podcast_stats
WSGIScriptAlias / /home/podstatsclub/webapp/Podcast_stats/podcast/wsgi.py
In your static directory you accidentally grant access only to wsgi.py ... thats a copy paste mistake. Just leave the "require all granted" there.

Can I create a flask webapp in the same url with other pages(like wordpress)?

I have a self-hosted server, only for my lan, with a Wordpress(miservidor.com) and Owncloud(miservidor.com/owncloud) page, those pages work perfectly, and i recentlly decided to create a webapp with flask under the same domain like miservidor.com/musicdownloader. I have tried to make it works but with no results, my config files are these:
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName miservidor.com
Redirect / https://miservidor.com/
</VirtualHost>
/etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<Directory />
AllowOverride ALL
</Directory>
</VirtualHost>
</IfModule>
/etc/apache2/config-available/musicdownloader.conf
<VirtualHost *:80>
WSGIScriptAlias /musicdownloader /var/www/musicdownloader/musicdownloader.wsgi
<Directory /var/www/musicdownloader/musicdownloader/>
Order allow,deny
Allow from all
</Directory>
Alias /musicdownloader/static /var/www/musicdownloader/musicdownloader/static
<Directory /var/www/musicdownloader/musicdownloader/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
/etc/apache2/config-available/owncloud.conf
Alias /owncloud "/var/www/owncloud/"
<Directory /var/www/owncloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
SetEnv HOME /var/www/owncloud
SetEnv HTTP_HOME /var/www/owncloud
</Directory>
My web app hierarchy is (in /var/www/):
|--musicdownloader/
|----musicdownloader.wgsi
|----musicdownloader/
|------app.py
|------static/
|--------main.css
|------templates/
|--------musicdownloader.html
|--------download.html
musicdownloader.wgsi
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/musicdownloader/")
from musicdownloader import app as application
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/musicdownloader', methods=["POST","GET"])
def musicdownloader():
return render_template('musicdownloader.html')
if __name__ == "__main__":
app.run()
Is this the correct way to do it? It is better to use subdomains or different domains with the same ip? Thanks in advance.
I found the solution, I can do it by changing the content I put in the file /etc/apache2/config-available/musicdownloader.conf to:
WSGIScriptAlias /musicdownloader /var/www/musicdownloader/musicdownloader.wgsi
WSGIDaemonProcess musicdownloader python-path=/var/www/musicdownloader:/var/www/musicdownloader/musicdownloader/venv/lib/python3.8/site-packages
WSGIProcessGroup musicdownloader
<Directory /var/www/musicdownloader/musicdownloader/>
Require all granted
</Directory>
Alias /musicdownloader/static /var/www/musicdownloader/musicdownloader/static
<Directory /var/www/musicdownloader/musicdownloader/static/>
Require all granted
</Directory>
Then:
sudo systemctl reload apache2
And now I can have my flask app in:
miservidor.com/musicdownloader

Django 2.0 Static Files not recognized in Apache2

My website is online, but it can't find the static files ;/
The configurations seem to be all ok, can't find the error...
I don't have admin permission on the server, so it kind'a sucks..
but, at least they pass me the configurations they've put..
Apache Configuration:
<VirtualHost *>
ServerAdmin suporte#pop-rs.rnp.br
DocumentRoot /home/metis/public_html/
ServerName agata.pgie.ufrgs.br
ServerAlias www.agata.pgie.ufrgs.br
#ErrorLog /var/log/apache2/agata-error.log
#CustomLog /var/log/apache2/agata-access.log common
<Directory /home/metis/public_html/static>
Require all granted
</Directory>
<Directory /home/metis/public_html/AGATA/textMiningProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess agata python-path=/home/metis/public_html/AGATA python-home=/home/metis/public_html/AGATA/agataenv
WSGIProcessGroup agata
WSGIScriptAlias / /home/metis/public_html/AGATA/textMiningProject/wsgi.py
</VirtualHost>
Django Version: 2.0
settings.py
Static files (CSS, JavaScript, Images)
https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/' # You may find this is already defined as such.
STATIC_ROOT = '/home/metis/public_html/static'
STATICFILES_DIRS = [
STATIC_PATH
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
And it was asked to the admins to insert the following, no changes in the result tho..
Alias /media/ /home/metis/public_html/AGATA/media
Alias /static/ /home/metis/public_html/static
<Directory /home/metis/public_html/static>
…
Order allow,deny
Allow from all
</Directory>
can someone help?
Edit 1:
The output for collecstatic:
0 static files copied to '/home/metis/public_html/static', 166
unmodified.
The error on the site:
simple-sidebar.css Failed to load resource: the server responded with
a status of 404 (Not Found)

Serving static files through apache

I am new to the whole mod_wsgi and serving files through apache. I am really comfortable with flask but this is something i can't get my head around. I did the hello-world program and successfully displayed hello world!
Now i wanted to display a image file. So I updated my hello-world.py to:
from flask import *
yourflaskapp = Flask(__name__)
#yourflaskapp.route("/")
def hello():
file="203.jpg"
return render_template("hello.html",file=file)
# return"HEY"
if __name__ == "__main__":
yourflaskapp.run()
my directory structure is something like:/var/www/hello-world
/hello-world
test.py
yourflaskapp.wsgi
/static
-203.jpg
/templates
-hello.html
My template is simple:
<!DOCTYPE html>
<html><head><title>hi</title></head>
<body>
<img src="{{url_for('static',filename=file)}}"/>
</body></html>
and my apache conf file is:
<VirtualHost *:80>
WSGIDaemonProcess yourflaskapp
WSGIScriptAlias / /var/www/hello-world/yourflaskapp.wsgi
Alias /static/ /var/www/hello-world/static
Alias /templates/ /var/www/hello-world/templates
<Directory /var/www/hello-world>
WSGIProcessGroup yourflaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/hello-world/static>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/hello-world/templates>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Though when i open the browser and head to my ip, it doesn't show the image file.
What am i doing wrong? Is there any other approach i should follow?
and if anyone could recommend any good links from where i can understand working with flask+mod_wsgi+apache2
It is generally always a good idea to have trailing slash balanced when mounting static files at a sub URL. So instead of:
Alias /static/ /var/www/hello-world/static
use:
Alias /static /var/www/hello-world/static

Apache, Web.py and Mod_WSGI Issue - Page Not Found

Please your help to know what is wrong with my configuration Apache + Web.py + Mod_WSGI and my website cannot be loaded, this is my config:
Apache version: 2.7.4
Web.py version -> https://github.com/webpy/webpy
mod_wsgi -> not sure if 3.3 or 3.4
Directory
/var/www/pyapps/bot
web -> link webpy
webpy
code.py
static
templates
Apache
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
CustomLog ${APACHE_LOG_DIR}/access.log combine
WSGIScriptAlias /sitea /var/www/pyapps/bot/code.py/
Alias /sitea/static /var/www/pyapps/bot/static
Alias /sitea/templates /var/www/pyapps/bot/templates
AddType text/html .py
<Directory /var/www/pyapps/bot>
Order deny,allow
Allow from All
</Directory>
<Directory /sitea/static>
Options +Indexes
</Directory>
Code.py
import web
urls = (
'/.*', 'hello',
)
class hello:
def GET(self):
return "Hello, world."
application = web.application(urls, globals()).wsgifunc()
Result
Not Found
The requested URL /sitea was not found on this server.
Apache/2.2.22 (Ubuntu) Server at xxx.xxx.xxx.xxx Port 80
You shouldn't have a trailing slash on the end of the WSGIScriptAlias path:
WSGIScriptAlias /sitea /var/www/pyapps/bot/code.py
Also note you shouldn't really have your WSGI application files inside the document root (/var/www). Put them somewhere else (and change the WSGIScriptAlias path accordingly).

Categories

Resources