Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Behr, Svenja
Django_Blog
Commits
4297cd8d
Commit
4297cd8d
authored
Jun 30, 2020
by
Tang, Alexander
Browse files
Merge branch 'Alex_2020_06_23' into 'master'
Alex 2020 06 23 See merge request
!13
parents
21c790de
c8f1eb6c
Changes
3
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
4297cd8d
...
...
@@ -97,7 +97,7 @@ Open the link http://127.0.0.1:8000/ and check, if the page opens up and shows s
**Note!**
If you see a message, telling you to perform manage.py commands, something went wrong, as the
start page content could not be found. Try to redo the steps above, please.
#
AB HIER NOCH SAUBER MACHEN
#
Intern oft verwendete Befehle
### Django Befehle
#### Neues Module (App)
`manage.py startapp *appname*`
...
...
@@ -112,118 +112,3 @@ start page content could not be found. Try to redo the steps above, please.
`manage.py migrate`
Achtung: Wenigstens einmal auf alles, sonst gehts nicht.
#### Admin-Nutzer
`manage.py createsuperuser`
|(changepassword)
#### Django-Klassen -> Datenbankmodel
https://docs.djangoproject.com/en/3.0/ref/models/fields/
in unserem Projekt vielleicht so etwas wie
```
class Blog(models.Model)
title = models.CharField(max_length=80) #maximal 80 Zeichen
image = models.ImageField(upload_to='...') #Wenn Bilder -> pillow ext laden
```
#### Dann Model in admin.py registrieren
```
# Register your models here.
from .models import Blog
admin.site.register(Blog)
```
#### settings.py -> Medienordner setzen
```
MEDIA_URL = '/media/' # kann natuerlich auch anders benannt werden
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')` # speichert Medien im Ordner 'media ab'
```
#### Statische Bilder anzeigen lassen urls.py
```
<...>
from django.conf.urls.static import static
from django.conf import settings
<...>
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
```
#### Prozess um weitere Seiten einzubinden
**1. Setting up URL**
<br>
urls.py: add path and import views
<br>
**2. Setting up View (eigentlich der Controller)**
<br>
create new function in views.py, e.g. def home(request):
<br>
**3. Return a template (eigentlich die View)**
<br>
create directories in project folder templates/...
<br>
create html file
<br>
#### Datenbank-Daten (Objekte) in Seite einbinden
**1. views.py**
```
from .models import Blog
<...>
def home(request):
blogObjects = Blog.objects().all # grabs all objects from the DB table 'Blog'
return render(request, '<Website e.g. <App folder>/home.html', {'blogobjects':blogobjects})
--> damit bekommt man ein QuerySet zurückgegeben
```
**2. home.html**
```
{% for blogobject in blogobjects %}
{{ blogobject}}
{% endfor %}
```
#### CKEditor Befehle
**1. Installation**
```
pip install django-ckeditor
```
**2. Vorbereitende Maßnahmen (schon durchgeführt und nicht nochmal nötig)**
Für CKEditor ohne Uploader
-Add ckeditor to your INSTALLED_APPS setting.
-Run the collectstatic management command:
```
manage.py collectstatic
```
This will copy static CKEditor required media resources into the directory given by the STATIC_ROOT setting.
See Django’s documentation on managing static files for more info.
Zusätzlich für CKEditor mit Uploader
-Add ckeditor_uploader to your INSTALLED_APPS setting.
-Add a CKEDITOR_UPLOAD_PATH setting to the project’s settings.py file. This setting specifies a relative path to
your CKEditor media upload directory. CKEditor uses Django’s storage API. By default, Django uses the file system
storage backend (it will use your MEDIA_ROOT and MEDIA_URL) and if you don’t use a different backend you have to
have write permissions for the CKEDITOR_UPLOAD_PATH path within MEDIA_ROOT, i.e.:
```
CKEDITOR_UPLOAD_PATH = "uploads/"
```
When using default file system storage, images will be uploaded to “uploads” folder in your
MEDIA_ROOT and urls will be created against MEDIA_URL (/media/uploads/image.jpg).
**3. Benutzung**
Field
The quickest way to add rich text editing capabilities to your models is to use the included RichTextField model
field type. A CKEditor widget is rendered as the form field but in all other regards the field behaves as the
standard Django TextField. For example:
```
from django.db import models
from ckeditor.fields import RichTextField
class Post(models.Model):
content = RichTextField()
```
For file upload support use RichTextUploadingField from ckeditor_uploader.fields.
**4. Doc**
Mehr unter: https://django-ckeditor.readthedocs.io/en/latest/#usage
\ No newline at end of file
blog/templates/blog/all_blogs.html
View file @
4297cd8d
...
...
@@ -15,7 +15,7 @@
{% block content %}
<div
class=
"container-fluid"
>
<div
class=
"row mb-4 mx-4"
>
{% for blog in blogs %}
{% for blog in
overview_
blogs %}
<div
class=
"col-sm-12 col-lg-4 px-2 mb-4"
>
<a
class=
"card"
href=
"{{ blog.slug }}"
>
{% if blog.image and blog.image.url %}
...
...
blog/views.py
View file @
4297cd8d
...
...
@@ -2,6 +2,9 @@ from django.http import HttpResponseNotAllowed, Http404
from
django.contrib
import
messages
from
django.shortcuts
import
render
,
get_object_or_404
,
redirect
from
django.urls
import
reverse_lazy
from
itertools
import
chain
from
random
import
shuffle
from
django.contrib.auth
import
authenticate
,
login
,
logout
...
...
@@ -24,7 +27,7 @@ def get_all_blogs(request):
f
' AND status &
{
BLOG_STATE_CAN_HAVE_COMMENTS_AND_USER_ONLY
}
'
\
f
'<>
{
BLOG_STATE_CAN_HAVE_COMMENTS_AND_USER_ONLY
}
'
blogs
=
Blog
.
objects
.
extra
(
where
=
[
query
]).
order_by
(
'created'
)
blogs
=
Blog
.
objects
.
extra
(
where
=
[
query
]).
order_by
(
'
-
created'
)
return
blogs
...
...
@@ -33,9 +36,19 @@ def all_blogs(request):
""" Show all blog articles in an overview list.
Will exclude those which should be hidden in this list (e.g. impress)
"""
# Exclude those with status hidden from the overall list
query
=
f
'status &
{
BLOG_STATE_HIDE_FROM_LIST
}
<>
{
BLOG_STATE_HIDE_FROM_LIST
}
'
allblogs
=
get_all_blogs
(
request
)
# split into subqueries, turn into lists for shuffling, then being chained
if
allblogs
.
count
()
>
3
:
last_three_blogs
=
allblogs
[:
3
]
all_blogs_except_last_three_in_random_order
=
list
(
allblogs
[
3
:])
shuffle
(
all_blogs_except_last_three_in_random_order
)
allblogs
=
chain
(
last_three_blogs
,
all_blogs_except_last_three_in_random_order
)
blogs
=
get_all_blogs
(
request
)
return
render
(
request
,
'blog/all_blogs.html'
,
{
'blogs'
:
blogs
})
return
render
(
request
,
'blog/all_blogs.html'
,
{
'blogs'
:
blogs
,
'overview_blogs'
:
allblogs
})
def
detail
(
request
,
slug
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment