From 7247681ebb67f2f4c98ee7c5b50ba14fdb4da1eb Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sat, 30 Dec 2017 19:01:04 +0100 Subject: [PATCH 1/6] readd two commands I removed the lines by accident they broke the apache configuration --- Vagrantfile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Vagrantfile b/Vagrantfile index a8fe782..dc33c61 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -32,6 +32,12 @@ Vagrant.configure("2") do |config| libnss-mdns libapache2-mod-wsgi-py3 python3-mysqldb python3-pip pip3 install django-extensions Pillow pyaml + #Copy the apache configuration for django to the correct place + cp /vagrant/apache/000-default.conf /etc/apache2/sites-available/ + + #restart the webserver + systemctl restart apache2.service + /vagrant/ansible/roles/web_AI-5/tasks/setup_script.sh SHELL From 559f77545dc072073332f975b1c51c0cca33af97 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 31 Dec 2017 15:07:22 +0100 Subject: [PATCH 2/6] move the media root and add support for MEDIA_URL --- Vagrantfile | 3 +++ django/didgeridoo/didgeridoo/settings.py | 5 ++++- django/didgeridoo/didgeridoo/urls.py | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index dc33c61..f32fe7a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -35,6 +35,9 @@ Vagrant.configure("2") do |config| #Copy the apache configuration for django to the correct place cp /vagrant/apache/000-default.conf /etc/apache2/sites-available/ + mkdir /srv/media + chmod -R 777 /srv/media + #restart the webserver systemctl restart apache2.service diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index 3c31a02..b811f2a 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -68,6 +68,7 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'django.template.context_processors.media' ], }, }, @@ -132,6 +133,8 @@ USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = '/vagrant/django/didgeridoo/static/' -MEDIA_ROOT = '/vagrant/django/didgeridoo/media/' + +MEDIA_URL = '/media/' +MEDIA_ROOT = '/srv/media/' LOGIN_REDIRECT_URL = '/' diff --git a/django/didgeridoo/didgeridoo/urls.py b/django/didgeridoo/didgeridoo/urls.py index 1eef47b..be44997 100644 --- a/django/didgeridoo/didgeridoo/urls.py +++ b/django/didgeridoo/didgeridoo/urls.py @@ -15,9 +15,11 @@ Including another URLconf """ from django.conf.urls import include, url from django.contrib import admin +from django.conf import settings +from django.conf.urls.static import static urlpatterns = [ url(r'', include('webshop.urls')), url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls), -] +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) From 3b73c67c66fd66b08e27241f04154811cb5f28da Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 31 Dec 2017 15:09:36 +0100 Subject: [PATCH 3/6] add pictures to the article details page --- apache/000-default.conf | 4 ++-- .../webshop/templates/webshop/article_details.html | 3 +++ django/didgeridoo/webshop/views.py | 11 +++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/apache/000-default.conf b/apache/000-default.conf index bf85cc0..b9d347e 100644 --- a/apache/000-default.conf +++ b/apache/000-default.conf @@ -13,7 +13,7 @@ WSGIPythonPath /vagrant/django/didgeridoo/ ServerAdmin webmaster@localhost - Alias /media/ /vagrant/django/didgeridoo/media/ + Alias /media/ /srv/media/ Alias /static/ /vagrant/django/didgeridoo/static/ @@ -23,7 +23,7 @@ WSGIPythonPath /vagrant/django/didgeridoo/ - + Require all granted diff --git a/django/didgeridoo/webshop/templates/webshop/article_details.html b/django/didgeridoo/webshop/templates/webshop/article_details.html index e580fdc..3c1c3bb 100644 --- a/django/didgeridoo/webshop/templates/webshop/article_details.html +++ b/django/didgeridoo/webshop/templates/webshop/article_details.html @@ -6,4 +6,7 @@

Stock: {{ article.stock }}

Status: {{ article.status}}

Price: {{ article.price_in_chf }}

+ {% for picture in picture_list %} +

+ {% endfor %} {% endblock %} diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 499c261..7d921a6 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -3,7 +3,12 @@ from django.shortcuts import get_object_or_404, render from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm -from webshop.models import Article, Category, ArticleStatus, Person, City +from webshop.models import (Article, + Category, + ArticleStatus, + Person, + City, + Picture) from webshop.forms import RegistrationForm # Create your views here. @@ -36,8 +41,10 @@ def articles_in_category(request, category_id): def article_details(request, article_id): article = get_object_or_404(Article, pk=article_id) + picture_list = Picture.objects.filter(article=article_id) return render(request, 'webshop/article_details.html', - {'article': article}) + {'article': article, + 'picture_list': picture_list}) @login_required From 29d566cf93a41bf5d39e034cc7c0107bd88e988a Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 31 Dec 2017 15:18:16 +0100 Subject: [PATCH 4/6] create the media directory on the production server --- ansible/roles/web_AI-5/tasks/main.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ansible/roles/web_AI-5/tasks/main.yml b/ansible/roles/web_AI-5/tasks/main.yml index 4cab145..025855b 100644 --- a/ansible/roles/web_AI-5/tasks/main.yml +++ b/ansible/roles/web_AI-5/tasks/main.yml @@ -34,5 +34,13 @@ - name: Run the setup script to add some final touches shell: "/vagrant/ansible/roles/web_AI-5/tasks/setup_script.sh" +- name: Creates directory + file: + path: /srv/media + state: directory + owner: www-data + group: www-data + mode: 0755 + - name: Restart apache service service: name=apache2 state=restarted From fef0526bc9dd2067ee77f55988d05705d96a9d20 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 31 Dec 2017 15:22:00 +0100 Subject: [PATCH 5/6] create the path /srv/media/images in the Vagrant vm This is necessary due to permissions problems. If apache server creates the directory the django server looses permission to write in it. This is only a problem during development in production apache is the only running webserver. --- Vagrantfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vagrantfile b/Vagrantfile index f32fe7a..cea707a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -35,7 +35,7 @@ Vagrant.configure("2") do |config| #Copy the apache configuration for django to the correct place cp /vagrant/apache/000-default.conf /etc/apache2/sites-available/ - mkdir /srv/media + mkdir -p /srv/media/images chmod -R 777 /srv/media #restart the webserver From 877b536e4a99a3ca52dd1ebb25f91ce6cb645564 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 31 Dec 2017 15:23:31 +0100 Subject: [PATCH 6/6] remove the old media directory --- django/didgeridoo/media/images/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 django/didgeridoo/media/images/.keep diff --git a/django/didgeridoo/media/images/.keep b/django/didgeridoo/media/images/.keep deleted file mode 100644 index e69de29..0000000