diff --git a/khaikang/models.py b/khaikang/models.py new file mode 100644 index 0000000..fde83cf --- /dev/null +++ b/khaikang/models.py @@ -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', + ) + + diff --git a/khaikang/settings.py b/khaikang/settings.py index e86d144..18377a9 100644 --- a/khaikang/settings.py +++ b/khaikang/settings.py @@ -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' \ No newline at end of file diff --git a/khaikang/urls.py b/khaikang/urls.py index 75da5d0..9f14fc4 100644 --- a/khaikang/urls.py +++ b/khaikang/urls.py @@ -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'), ] diff --git a/khaikang/views.py b/khaikang/views.py new file mode 100644 index 0000000..59ae0ec --- /dev/null +++ b/khaikang/views.py @@ -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)) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..13dd62f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,12 @@ +{% extends "base_generic.html" %} + +{% block content %} +
+{% endblock %} \ No newline at end of file diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 0000000..6024127 --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,39 @@ +{% extends "base_generic.html" %} + + +{% block content %} + +{% if form.errors %} +Your username and password didn't match. Please try again.
+{% endif %} + + + {% if user.is_authenticated %} +You don't have to log in again since you have logged in.
+ + {% else %} +Please login:
+ + + + +{# Assumes you setup the password_reset view in your URLconf #} + + +{% endif %} +{% endblock %} \ No newline at end of file