add home, and basic model

This commit is contained in:
Tan, Kian-ting 2022-11-11 01:37:16 +08:00
parent 049ff2300c
commit 9511f19284
6 changed files with 132 additions and 2 deletions

57
khaikang/models.py Normal file
View file

@ -0,0 +1,57 @@
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
class User(AbstractBaseUser):
"""customized user field."""
username = models.CharField(max_length=30, unique=True) # max to 30 char
USERNAME_FIELD = 'username' # setting username field to "id"
created_time = models.DateTimeField()
shown_name = models.CharField(max_length=50) # max to 50 char
url = models.URLField(max_length=200) # max to 200 char
desc = models.TextField() # description
GROUP_CLASSES = [
('admin', 'admin'),
('sysop', 'sysop'),
('general', 'general'),
('suspended', 'suspended'),
('deleted', 'deleted'),
]
classfication = models.CharField(
max_length=9,
choices=GROUP_CLASSES,
default='general',
)
email = models.EmailField(max_length=200) # max to 200 char
REQUIRED_FIELDS = ['id', 'shown_name', 'classfication', 'email']
class Following(models.Model):
follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name="follower")
followee = models.ForeignKey(User, on_delete=models.CASCADE, related_name="followee")
class Post(models.Model):
text = models.TextField()
poster = models.ForeignKey(User, on_delete=models.CASCADE)
post_time = models.DateTimeField()
GROUP_PRIVILAGES = [
('public', 'public'), # post to public timeline
('unpublic', 'unpublic'), # not post to public timeline
('private', 'private'), # to followers
]
privilage = models.CharField(
max_length=8,
choices=GROUP_PRIVILAGES,
default='public',
)

View file

@ -31,6 +31,7 @@ DEBUG = True
# Application definition
INSTALLED_APPS = [
'khaikang',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
@ -54,7 +55,7 @@ ROOT_URLCONF = 'khaikang.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': ['templates',],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@ -122,3 +123,6 @@ STATIC_ROOT = '/home/kiantin1/public_html/khaikang/static/'
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# automatically redirect to home page (/home) after login.
LOGIN_REDIRECT_URL = '/home'

View file

@ -16,8 +16,10 @@ Including another URLconf
from django.contrib import admin
from django.urls import path
from django.urls import include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('django.contrib.auth.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('home/', views.home, name='home'),
]

16
khaikang/views.py Normal file
View file

@ -0,0 +1,16 @@
from django.http import HttpResponse
from django.template import loader
from .models import User, Post
def home(request):
public_timeline_list = Post.objects.filter(privilage = 'public')[:10]
print(public_timeline_list)
template = loader.get_template('index.html')
context = {
'latest_question_list': public_timeline_list,
}
return HttpResponse(template.render(context, request))

12
templates/index.html Normal file
View file

@ -0,0 +1,12 @@
{% extends "base_generic.html" %}
{% block content %}
<form method="POST" id="posting-form">
{% csrf_token %}
<div class="posting-form-group">
<label>Post</label>
<input type="text" class="form-post" id="post_text" placeholder="What do you want to post?">
</div>
<button type="submit_post" class="btn">Post!</button>
</form>
{% endblock %}

View file

@ -0,0 +1,39 @@
{% extends "base_generic.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if user.is_authenticated %}
<p>You don't have to log in again since you have logged in.</p>
<p><a href="/home">Go to main page.</a></p>
{% else %}
<p>Please login:</p>
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<div>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</div>
<div>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</div>
<div>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</div>
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endif %}
{% endblock %}