Skip to content

Commit

Permalink
Add Post model and basic listing page
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpg committed Jun 24, 2024
1 parent 3961dc6 commit df934d1
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sample_taggit/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Post

admin.site.register(Post)
43 changes: 43 additions & 0 deletions sample_taggit/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 5.0.6 on 2024-06-24 12:35

import taggit.managers
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
(
"taggit",
"0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx",
),
]

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=200)),
(
"tags",
taggit.managers.TaggableManager(
help_text="A comma-separated list of tags.",
through="taggit.TaggedItem",
to="taggit.Tag",
verbose_name="Tags",
),
),
],
),
]
Empty file.
9 changes: 9 additions & 0 deletions sample_taggit/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
from django.db import models

from taggit.managers import TaggableManager


class Post(models.Model):
title = models.CharField(max_length=200)
tags = TaggableManager(blank=True)
2 changes: 2 additions & 0 deletions sample_taggit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"taggit",
"sample_taggit",
]

MIDDLEWARE = [
Expand Down
6 changes: 6 additions & 0 deletions sample_taggit/temlates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<title>Sample Taggit App</title>
<body>
<h1>Sample Taggit App</h1>
</body>
</html>
24 changes: 24 additions & 0 deletions sample_taggit/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<title>Sample Taggit App</title>
<body>
<h1>Sample Taggit App</h1>

<ul>
{% for post in posts %}
<li>
{{ post.title }}
<br/>
Tags:
{% for tag in post.tags.all %}
{{tag.name}},
{% empty %}
(None)
{% endfor %}
</li>
{% empty %}
<li>(No posts yet, go to the admin to create some)</li>
{% endfor %}
</ul>
<a href="/admin">Go to admin and manage more data</a>
</body>
</html>
6 changes: 3 additions & 3 deletions sample_taggit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
from django.contrib import admin
from django.urls import path

urlpatterns = [
path("admin/", admin.site.urls),
]
from sample_taggit.views import index_view

urlpatterns = [path("admin/", admin.site.urls), path("", index_view)]
7 changes: 7 additions & 0 deletions sample_taggit/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.shortcuts import render

from sample_taggit.models import Post


def index_view(request):
return render(request, "index.html", {"posts": Post.objects.all()})

0 comments on commit df934d1

Please sign in to comment.