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

Won Posts & Comments Solution #1

Open
wants to merge 2 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
Binary file added posts_and_comments/db.sqlite3
Binary file not shown.
22 changes: 22 additions & 0 deletions posts_and_comments/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'posts_and_comments.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
3 changes: 3 additions & 0 deletions posts_and_comments/posts/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.
6 changes: 6 additions & 0 deletions posts_and_comments/posts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PostsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'posts'
31 changes: 31 additions & 0 deletions posts_and_comments/posts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 3.2.4 on 2021-06-27 23:58

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


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=64)),
('body', models.CharField(max_length=600)),
],
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.CharField(max_length=300)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='posts.post')),
],
),
]
Empty file.
15 changes: 15 additions & 0 deletions posts_and_comments/posts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import models
from django.db.models.fields.related import ForeignKey


# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=64)
body = models.CharField(max_length=600)


class Comment(models.Model):
body = models.CharField(max_length=300)
post = models.ForeignKey('Post',
on_delete=models.CASCADE,
related_name='comments')
11 changes: 11 additions & 0 deletions posts_and_comments/posts/templates/posts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>My Posts</h1>

{% if posts_list %}
<ul>
{% for post in posts_list %}
<li><a href="/posts/{{ post.id }}/">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No posts yet!</p>
{% endif %}
8 changes: 8 additions & 0 deletions posts_and_comments/posts/templates/posts/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>{{current_post.title}}</h1>

<ul>
{% comment %} in django, no need to do .all(). do 'all' instead {% endcomment %}
{% for comment in current_post.comments.all %}
<li>{{comment.body}}</li>
{% endfor %}
</ul>
3 changes: 3 additions & 0 deletions posts_and_comments/posts/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.
5 changes: 5 additions & 0 deletions posts_and_comments/posts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.urls import path

from . import views

urlpatterns = [path('', views.index, name='index'), path('<int:id>/', views.post, name='post')]
5 changes: 5 additions & 0 deletions posts_and_comments/posts/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .models import Post, Comment


def get_post_by_id(post_id):
return Post.objects.get(id=post_id)
18 changes: 18 additions & 0 deletions posts_and_comments/posts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.shortcuts import render
from django.http import HttpResponse
from .models import Post, Comment
from .util import get_post_by_id

# Create your views here.


def index(request):
posts_list = Post.objects.all()
context = {'posts_list': posts_list}
return render(request, 'posts/index.html', context)


def post(request, id):
current_post = get_post_by_id(id)
context = {'current_post': current_post}
return render(request, 'posts/post.html', context)
Empty file.
16 changes: 16 additions & 0 deletions posts_and_comments/posts_and_comments/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for posts_and_comments project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'posts_and_comments.settings')

application = get_asgi_application()
19 changes: 19 additions & 0 deletions posts_and_comments/posts_and_comments/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""posts_and_comments URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [path('posts/', include('posts.urls'), name='posts')]
16 changes: 16 additions & 0 deletions posts_and_comments/posts_and_comments/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for posts_and_comments project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'posts_and_comments.settings')

application = get_wsgi_application()