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

Lesson 32 #10

Open
wants to merge 32 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
Empty file.
3 changes: 3 additions & 0 deletions djangonautic/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions djangonautic/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
name = 'accounts'
Empty file.
3 changes: 3 additions & 0 deletions djangonautic/accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
14 changes: 14 additions & 0 deletions djangonautic/accounts/templates/accounts/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base_layout.html' %}

{% block content %}
<h1>Log in</h1>
<form class="site-form" action="{% url 'accounts:login' %}" method="post">
{% csrf_token %}
{{ form }}
{% if request.GET.next %}
<input type="hidden" name="next" value="{{ request.GET.next }}" />
{% endif %}
<input type="submit" value="Login" />
</form>
<p>Not got an account? <a href="{% url 'accounts:signup' %}">Sign Up</a></p>
{% endblock %}
10 changes: 10 additions & 0 deletions djangonautic/accounts/templates/accounts/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base_layout.html' %}

{% block content %}
<h1>Signup</h1>
<form class="site-form" action="{% url 'accounts:signup' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Signup">
</form>
{% endblock %}
3 changes: 3 additions & 0 deletions djangonautic/accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions djangonautic/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf.urls import url
from . import views

app_name = 'accounts'

urlpatterns = [
url(r'^signup/$', views.signup_view, name="signup"),
url(r'^login/$', views.login_view, name="login"),
url(r'^logout/$', views.logout_view, name="logout"),
]
35 changes: 35 additions & 0 deletions djangonautic/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login, logout

def signup_view(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
# log the user in
login(request, user)
return redirect('articles:list')
else:
form = UserCreationForm()
return render(request, 'accounts/signup.html', { 'form': form })

def login_view(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
# log the user in
user = form.get_user()
login(request, user)
if 'next' in request.POST:
return redirect(request.POST.get('next'))
else:
return redirect('articles:list')
else:
form = AuthenticationForm()
return render(request, 'accounts/login.html', { 'form': form })

def logout_view(request):
if request.method == 'POST':
logout(request)
return redirect('articles:list')
Empty file.
4 changes: 4 additions & 0 deletions djangonautic/articles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Article

admin.site.register(Article)
5 changes: 5 additions & 0 deletions djangonautic/articles/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ArticlesConfig(AppConfig):
name = 'articles'
7 changes: 7 additions & 0 deletions djangonautic/articles/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django import forms
from . import models

class CreateArticle(forms.ModelForm):
class Meta:
model = models.Article
fields = ['title', 'body', 'slug', 'thumb',]
26 changes: 26 additions & 0 deletions djangonautic/articles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-10 10:23
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('slug', models.SlugField()),
('body', models.TextField()),
('date', models.DateTimeField(auto_now_add=True)),
],
),
]
20 changes: 20 additions & 0 deletions djangonautic/articles/migrations/0002_article_thumb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-12 08:31
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('articles', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='article',
name='thumb',
field=models.ImageField(blank=True, default='/media/default.png', upload_to=''),
),
]
20 changes: 20 additions & 0 deletions djangonautic/articles/migrations/0003_auto_20171112_0905.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-12 09:05
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('articles', '0002_article_thumb'),
]

operations = [
migrations.AlterField(
model_name='article',
name='thumb',
field=models.ImageField(blank=True, default='default.png', upload_to=''),
),
]
23 changes: 23 additions & 0 deletions djangonautic/articles/migrations/0004_article_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-13 16:02
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('articles', '0003_auto_20171112_0905'),
]

operations = [
migrations.AddField(
model_name='article',
name='author',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
Empty file.
17 changes: 17 additions & 0 deletions djangonautic/articles/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField()
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
thumb = models.ImageField(default='default.png', blank=True)
author = models.ForeignKey(User, default=None)

def __str__(self):
return self.title

def snippet(self):
return self.body[:50] + '...'
13 changes: 13 additions & 0 deletions djangonautic/articles/templates/articles/article_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base_layout.html' %}

{% block content %}
<div class="create-article">
<h2>Create an Awesome New Article</h2>
<form class="site-form" action="{% url 'articles:create' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create">
</form>
</div>
<script src="/static/slugify.js"></script>
{% endblock %}
13 changes: 13 additions & 0 deletions djangonautic/articles/templates/articles/article_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base_layout.html' %}

{% block content %}
<div class="article-detail">
<div class="article">
<img src="{{ article.thumb.url }}" />
<h2>{{ article.title }}</h2>
<h3>Written by {{ article.author.username }}</h3>
<p>{{ article.body }}</p>
<p>{{ article.date }}</p>
</div>
</div>
{% endblock %}
15 changes: 15 additions & 0 deletions djangonautic/articles/templates/articles/article_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base_layout.html' %}

{% block content%}
<h1>Articles List</h1>
<div class="articles">
{% for article in articles %}
<div class="article">
<h2><a href="{% url 'articles:detail' slug=article.slug %}">{{ article.title }}</a></h2>
<p>{{ article.snippet }}</p>
<p>{{ article.date }}</p>
<p class="author">added by {{ article.author.username }}</p>
</div>
{% endfor %}
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions djangonautic/articles/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions djangonautic/articles/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf.urls import url
from . import views

app_name = 'articles'

urlpatterns = [
url(r'^$', views.article_list, name="list"),
url(r'^create/$', views.article_create, name="create"),
url(r'^(?P<slug>[\w-]+)/$', views.article_detail, name="detail"),
]
28 changes: 28 additions & 0 deletions djangonautic/articles/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .models import Article
from django.contrib.auth.decorators import login_required
from . import forms

def article_list(request):
articles = Article.objects.all().order_by('date');
return render(request, 'articles/article_list.html', { 'articles': articles })

def article_detail(request, slug):
# return HttpResponse(slug)
article = Article.objects.get(slug=slug)
return render(request, 'articles/article_detail.html', { 'article': article })

@login_required(login_url="/accounts/login/")
def article_create(request):
if request.method == 'POST':
form = forms.CreateArticle(request.POST, request.FILES)
if form.is_valid():
# save article to db
instance = form.save(commit=False)
instance.author = request.user
instance.save()
return redirect('articles:list')
else:
form = forms.CreateArticle()
return render(request, 'articles/article_create.html', { 'form': form })
Binary file added djangonautic/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions djangonautic/assets/slugify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const titleInput = document.querySelector('input[name=title]');
const slugInput = document.querySelector('input[name=slug]');

const slugify = (val) => {

return val.toString().toLowerCase().trim()
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[\s\W-]+/g, '-') // Replace spaces, non-word characters and dashes with a single dash (-)

};

titleInput.addEventListener('keyup', (e) => {
slugInput.setAttribute('value', slugify(titleInput.value));
});
Binary file added djangonautic/assets/stars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading