Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'lesson7' #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions blogging/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@

from django.contrib import admin
from blogging.models import Post, Category

from blogging.models import Post, Category, ModelAdmin2


class ModelAdminClass(admin.TabularInline):
model = ModelAdmin2
extra = 1


class PostAdmin(admin.ModelAdmin):
inlines = (ModelAdminClass,)


class CategoryAdmin(admin.ModelAdmin):
exclude = ('posts',)


admin.site.register(Post)
admin.site.register(Category)
# and a new admin registration
admin.site.register(Post, PostAdmin)
admin.site.register(Category, CategoryAdmin)
# Register your models here.
7 changes: 7 additions & 0 deletions blogging/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.forms import ModelForm
from blogging.models import Comment

class MyCommentForm(ModelForm):
class Meta:
model = Comment
fields = ['title', 'text', 'notes']
22 changes: 22 additions & 0 deletions blogging/migrations/0004_modeladmin2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.1.1 on 2020-01-15 02:18

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('blogging', '0003_auto_20191104_1942'),
]

operations = [
migrations.CreateModel(
name='ModelAdmin2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blogging.Category')),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blogging.Post')),
],
),
]
22 changes: 22 additions & 0 deletions blogging/migrations/0005_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.1.1 on 2020-01-16 04:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blogging', '0004_modeladmin2'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('text', models.CharField(max_length=255)),
('notes', models.CharField(max_length=255)),
],
),
]
19 changes: 18 additions & 1 deletion blogging/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.db import models
from django.db import models # <-- This is already in the file
from django.contrib.auth.models import User
from django.urls import reverse


class Post(models.Model):
title = models.CharField(max_length=128)
Expand All @@ -9,6 +11,9 @@ class Post(models.Model):
modified_date = models.DateTimeField(auto_now=True)
published_date = models.DateTimeField(blank=True, null=True)

def get_absolute_url(self):
return reverse('blogging:detail', kwargs={'pk':self.pk})

def __str__(self):
return self.title

Expand All @@ -22,3 +27,15 @@ class Meta:

def __str__(self):
return self.name

class ModelAdmin2(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)

class Comment(models.Model):
title = models.CharField(max_length=100)
text = models.CharField(max_length=255)
notes = models.CharField(max_length=255)

def __str__(self): # __unicode__ on Python 2
return self.title
15 changes: 15 additions & 0 deletions blogging/templates/blogging/my_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<DOCTYPE html>
<html>
<head>
<title>edit</title>
</head>
<body>

<form method="post" >
{% csrf_token %}
{{form}}

<button type="submit" class="btn btn-default"&gt;Submit</button>
</form>
</body>
</html>
21 changes: 20 additions & 1 deletion blogging/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import loader
from django import forms
from django.utils import timezone
from blogging.forms import MyCommentForm

from blogging.models import Post

Expand All @@ -20,3 +23,19 @@ def list_view(request):
posts = published.order_by('-published_date')
context = {'posts': posts}
return render(request, 'blogging/list.html', context)

def add_model(request):

if request.method == "POST":
form = MyCommentForm(request.POST)
if form.is_valid():
model_instance = form.save(commit=False)
model_instance.timestamp = timezone.now()
model_instance.save()
return redirect('/')

else:

form = MyCommentForm()

return render(request, "my_template.html", {'form': form})
16 changes: 16 additions & 0 deletions mysite/production.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

import dj_database_url

from .settings import *


DATABASES = {
'default': dj_database_url.config(default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3'))
}

DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = [os.environ.get('ALLOWED_HOSTS'), 'localhost']
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
SECRET_KEY = os.environ.get('SECRET_KEY')
14 changes: 14 additions & 0 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
'django.contrib.staticfiles',
'polling',
'blogging',
'django.contrib.sites',
'allauth.socialaccount.providers.facebook',
'allauth',
'allauth.account',
'allauth.socialaccount',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -123,3 +128,12 @@

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'

AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)

SITE_ID = 1

LOGIN_REDIRECT_URL = "/profile/"
3 changes: 2 additions & 1 deletion mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.urls import path, include,re_path
from django.contrib.auth.views import LoginView, LogoutView


Expand All @@ -24,4 +24,5 @@
path('admin/', admin.site.urls),
path('login/', LoginView.as_view(template_name='login.html'), name="login"),
path('logout/', LogoutView.as_view(next_page='/'), name="logout"),
re_path(r'^accounts/', include('allauth.urls')),
]
56 changes: 56 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
asgiref==3.2.3
astroid==2.2.5
atomicwrites==1.3.0
attrs==18.2.0
backcall==0.1.0
beautifulsoup4==4.8.1
bs4==0.0.1
certifi==2019.11.28
chardet==3.0.4
click==6.7
colorama==0.4.1
coverage==4.5.3
decorator==4.3.0
defusedxml==0.6.0
dj-database-url==0.5.0
Django==2.1.1
django-allauth==0.41.0
Flask==1.0.2
gunicorn==20.0.4
idna==2.8
ipython==7.2.0
ipython-genutils==0.2.0
isort==4.3.17
itsdangerous==0.24
jedi==0.13.2
Jinja2==2.10
lazy-object-proxy==1.3.1
MarkupSafe==1.0
mccabe==0.6.1
more-itertools==6.0.0
numpy==1.16.2
oauthlib==3.1.0
parso==0.3.1
passlib==1.7.2
peewee==3.3.4
pickleshare==0.7.5
pluggy==0.8.1
prompt-toolkit==2.0.7
psycopg2==2.8.4
py==1.7.0
Pygments==2.3.1
pylint==2.3.1
pymongo==3.8.0
pytest==4.3.0
python3-openid==3.1.0
pytz==2019.3
requests==2.22.0
requests-oauthlib==1.3.0
six==1.12.0
soupsieve==1.9.5
sqlparse==0.3.0
traitlets==4.3.2
typed-ast==1.3.4
urllib3==1.25.7
virtualenv==16.7.8
wcwidth==0.1.7
Werkzeug==0.14.1
wrapt==1.11.1