add login and change setting a bit
This commit is contained in:
parent
fbd2116921
commit
8ac04f8bf0
21
blog/migrations/0004_alter_post_author.py
Normal file
21
blog/migrations/0004_alter_post_author.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# Generated by Django 5.1 on 2024-08-31 13:04
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('blog', '0003_rename_markdowncontent_post'),
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='post',
|
||||||
|
name='author',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,11 +1,12 @@
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
class Post(models.Model):
|
class Post(models.Model):
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
content = models.TextField()
|
content = models.TextField()
|
||||||
author = models.CharField(max_length=100,default='tbd')
|
author = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
created_at = models.DateTimeField(default=timezone.now)
|
created_at = models.DateTimeField(default=timezone.now)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>My Blog</title>
|
<title>My Blog is shiny</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>My Blog</h1>
|
<h1>My Blog</h1>
|
||||||
|
|
18
blog/templates/blog/dashboard.html
Normal file
18
blog/templates/blog/dashboard.html
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Dashboard</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>Dashboard</h2>
|
||||||
|
<p>Welcome, {{ request.user.username }}!</p>
|
||||||
|
<a href="{% url 'logout' %}">Logout</a>
|
||||||
|
<h3>Your Posts</h3>
|
||||||
|
<ul>
|
||||||
|
{% for post in posts %}
|
||||||
|
<li>{{ post.title }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
20
blog/templates/blog/login.html
Normal file
20
blog/templates/blog/login.html
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Login</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>Login</h2>
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<label for="username">Username:</label>
|
||||||
|
<input type="text" name="username" id="username" required>
|
||||||
|
<br>
|
||||||
|
<label for="password">Password:</label>
|
||||||
|
<input type="password" name="password" id="password" required>
|
||||||
|
<br>
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
<p>{{ error }}</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
14
blog/templates/blog/register.html
Normal file
14
blog/templates/blog/register.html
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Register</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>Register</h2>
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form.as_p }}
|
||||||
|
<button type="submit">Register</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -1,6 +1,9 @@
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render , redirect
|
||||||
from .models import Post
|
from .models import Post
|
||||||
|
from django.contrib.auth import authenticate, login, logout
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.contrib.auth.forms import UserCreationForm
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
posts = Post.objects.all()
|
posts = Post.objects.all()
|
||||||
|
@ -9,3 +12,34 @@ def home(request):
|
||||||
def post_detail(request, pk):
|
def post_detail(request, pk):
|
||||||
post = Post.objects.get(pk=pk)
|
post = Post.objects.get(pk=pk)
|
||||||
return render(request, 'blog/post_detail.html', {'post': post})
|
return render(request, 'blog/post_detail.html', {'post': post})
|
||||||
|
|
||||||
|
def login_view(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
username = request.POST['username']
|
||||||
|
password = request.POST['password']
|
||||||
|
user = authenticate(request, username=username, password=password)
|
||||||
|
if user is not None:
|
||||||
|
login(request, user)
|
||||||
|
return redirect('dashboard')
|
||||||
|
else:
|
||||||
|
return render(request, 'blog/login.html', {'error': 'Invalid credentials'})
|
||||||
|
return render(request, 'blog/login.html')
|
||||||
|
|
||||||
|
def logout_view(request):
|
||||||
|
logout(request)
|
||||||
|
return redirect('login')
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def dashboard(request):
|
||||||
|
posts = Post.objects.filter(author=request.user)
|
||||||
|
return render(request, 'blog/dashboard.html', {'posts': posts})
|
||||||
|
|
||||||
|
def register(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = UserCreationForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
form.save()
|
||||||
|
return redirect('login')
|
||||||
|
else:
|
||||||
|
form = UserCreationForm()
|
||||||
|
return render(request, 'blog/register.html', {'form': form})
|
||||||
|
|
|
@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/5.1/topics/settings/
|
||||||
For the full list of settings and their values, see
|
For the full list of settings and their values, see
|
||||||
https://docs.djangoproject.com/en/5.1/ref/settings/
|
https://docs.djangoproject.com/en/5.1/ref/settings/
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
|
@ -51,11 +51,13 @@ MIDDLEWARE = [
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'django_mblog.urls'
|
ROOT_URLCONF = 'django_mblog.urls'
|
||||||
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [os.path.join(BASE_DIR, 'templates')],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
|
@ -122,3 +124,9 @@ STATIC_URL = 'static/'
|
||||||
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
|
|
||||||
|
# other settings
|
||||||
|
LOGIN_URL = 'login'
|
||||||
|
LOGIN_REDIRECT_URL = 'dashboard'
|
||||||
|
|
||||||
|
|
|
@ -22,4 +22,8 @@ urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('', views.home, name='home'),
|
path('', views.home, name='home'),
|
||||||
path('post/<int:pk>/', views.post_detail, name='post_detail'),
|
path('post/<int:pk>/', views.post_detail, name='post_detail'),
|
||||||
|
path('login/', views.login_view, name='login'),
|
||||||
|
path('logout/', views.logout_view, name='logout'),
|
||||||
|
path('dashboard/', views.dashboard, name='dashboard'),
|
||||||
|
path('register/', views.register, name='register'),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue