Created
February 11, 2018 16:14
-
-
Save dura0ok/b5b7827f59665af12a293eb01f9f3db9 to your computer and use it in GitHub Desktop.
help
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django import forms | |
| from .models import Post | |
| class PostForm(forms.ModelForm): | |
| class Meta: | |
| model = Post | |
| fields = ('title', 'text', 'image') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends 'base.html' %} | |
| {% block content %} | |
| {% load static %} | |
| {% for post in posts %} | |
| <div class="post"> | |
| <h1><a href="/post/{{ post.id }}">{{ post.title }}</a></h1> | |
| <img class="img" src="{{ post.image.url }}"> | |
| <p>published: {{ post.published_date }}</p> | |
| <h4>{{ post.text|linebreaksbr|truncatechars:70 }}</h4> | |
| <a href="/post/{{ post.id }}">Читать далее</a> | |
| </div> | |
| {% empty %} | |
| <h4 id="empty">Нету статей,добавьте статью</h4> | |
| {% endfor %} | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import models | |
| from django.utils import timezone | |
| class Post(models.Model): | |
| author = models.ForeignKey('auth.User') | |
| title = models.CharField(max_length=200) | |
| text = models.TextField() | |
| image = models.ImageField(upload_to='img') | |
| created_date = models.DateTimeField( | |
| default=timezone.now) | |
| published_date = models.DateTimeField( | |
| blank=True, null=True) | |
| def publish(self): | |
| self.published_date = timezone.now() | |
| self.save() | |
| def __str__(self): | |
| return self.title |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Django settings for djangogirls project. | |
| Generated by 'django-admin startproject' using Django 1.11. | |
| For more information on this file, see | |
| https://docs.djangoproject.com/en/1.11/topics/settings/ | |
| For the full list of settings and their values, see | |
| https://docs.djangoproject.com/en/1.11/ref/settings/ | |
| """ | |
| import os | |
| # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | |
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| # Quick-start development settings - unsuitable for production | |
| # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ | |
| # SECURITY WARNING: keep the secret key used in production secret! | |
| SECRET_KEY = '#o(_-d_b$n-9us)zjwklqml^*zo%36bcpggsyj=$5devtaw1%g' | |
| # SECURITY WARNING: don't run with debug turned on in production! | |
| DEBUG = True | |
| ALLOWED_HOSTS = [] | |
| # Application definition | |
| INSTALLED_APPS = [ | |
| 'django.contrib.admin', | |
| 'django.contrib.auth', | |
| 'django.contrib.contenttypes', | |
| 'django.contrib.sessions', | |
| 'django.contrib.messages', | |
| 'django.contrib.staticfiles', | |
| 'blog', | |
| ] | |
| MIDDLEWARE = [ | |
| 'django.middleware.security.SecurityMiddleware', | |
| 'django.contrib.sessions.middleware.SessionMiddleware', | |
| 'django.middleware.common.CommonMiddleware', | |
| 'django.middleware.csrf.CsrfViewMiddleware', | |
| 'django.contrib.auth.middleware.AuthenticationMiddleware', | |
| 'django.contrib.messages.middleware.MessageMiddleware', | |
| 'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
| ] | |
| ROOT_URLCONF = 'djangogirls.urls' | |
| TEMPLATES = [ | |
| { | |
| 'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
| 'DIRS': [], | |
| 'APP_DIRS': True, | |
| 'OPTIONS': { | |
| 'context_processors': [ | |
| 'django.template.context_processors.debug', | |
| 'django.template.context_processors.request', | |
| 'django.contrib.auth.context_processors.auth', | |
| 'django.contrib.messages.context_processors.messages', | |
| ], | |
| }, | |
| }, | |
| ] | |
| WSGI_APPLICATION = 'djangogirls.wsgi.application' | |
| # Database | |
| # https://docs.djangoproject.com/en/1.11/ref/settings/#databases | |
| DATABASES = { | |
| 'default': { | |
| 'ENGINE': 'django.db.backends.sqlite3', | |
| 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | |
| } | |
| } | |
| # Password validation | |
| # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators | |
| AUTH_PASSWORD_VALIDATORS = [ | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | |
| }, | |
| { | |
| 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | |
| }, | |
| ] | |
| # Internationalization | |
| # https://docs.djangoproject.com/en/1.11/topics/i18n/ | |
| LANGUAGE_CODE = 'ru-RU' | |
| TIME_ZONE = 'Europe/Moscow' | |
| USE_I18N = True | |
| USE_L10N = True | |
| USE_TZ = True | |
| #static and media files :D | |
| _PATH = os.path.abspath(os.path.dirname(__file__)) | |
| MEDIA_ROOT = os.path.join(_PATH, 'files', 'media') | |
| MEDIA_URL = '/media/' | |
| STATIC_ROOT = os.path.join(_PATH, 'files', 'static') | |
| STATIC_URL = '/static/' | |
| STATICFILES_DIRS = ( | |
| os.path.join(_PATH, 'static'), | |
| ) | |
| STATICFILES_FINDERS = ( | |
| 'django.contrib.staticfiles.finders.FileSystemFinder', | |
| 'django.contrib.staticfiles.finders.AppDirectoriesFinder', | |
| ) | |
| ADMIN_MEDIA_PREFIX = '/static/admin/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.shortcuts import render, redirect | |
| from django.utils import timezone | |
| from .models import Post | |
| from .forms import PostForm | |
| from django.shortcuts import get_list_or_404, get_object_or_404 | |
| def post_list(request): | |
| posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') | |
| return render(request, 'index.html', {'posts': posts}) | |
| def getfull(request, id): | |
| context = dict(post=Post.objects.get(id=id)) | |
| return render(request, 'post.html', context=context) | |
| def post_new(request): | |
| if request.method == "POST": | |
| form = PostForm(request.POST, request.FILES) | |
| if form.is_valid(): | |
| post = form.save(commit=False) | |
| post.author = request.user | |
| post.published_date = timezone.now() | |
| post.save() | |
| return redirect('/') | |
| else: | |
| form = PostForm() | |
| return render(request, 'add.html', {'form': form}) | |
| def post_edit(request, pk): | |
| post = get_object_or_404(Post, pk=pk) | |
| if request.method == "POST": | |
| form = PostForm(request.POST, instance=post) | |
| if form.is_valid(): | |
| post = form.save(commit=False) | |
| post.author = request.user | |
| post.published_date = timezone.now() | |
| post.save() | |
| return redirect('/') | |
| else: | |
| form = PostForm(instance=post) | |
| return render(request, 'red.html', {'form': form}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment