-
-
Save r0path/dfb9c5657fc8b8d7c25b33d9cfcc6a26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
from django.db import models | |
from django.contrib.auth.models import User | |
class Post(models.Model): | |
title = models.CharField(max_length=200) | |
content = models.TextField() | |
author = models.ForeignKey(User, on_delete=models.CASCADE) | |
created_at = models.DateTimeField(auto_now_add=True) | |
def __str__(self): | |
return self.title | |
# views.py | |
from django.shortcuts import render, redirect | |
from django.contrib.auth import authenticate, login, logout | |
from django.contrib.auth.decorators import login_required | |
from django.views import View | |
from .models import Post | |
from django.contrib.auth.models import User | |
class UserViewSet(View): | |
def get(self, request): | |
users = User.objects.all() | |
return render(request, 'user_list.html', {'users': users}) | |
def post(self, request): | |
# Logic to create a new user | |
username = request.POST.get('username') | |
password = request.POST.get('password') | |
User.objects.create_user(username=username, password=password) | |
return redirect('user_list') | |
class PostViewSet(View): | |
def get(self, request): | |
posts = Post.objects.all() | |
return render(request, 'post_list.html', {'posts': posts}) | |
def post(self, request): | |
# Logic to create a new post | |
title = request.POST.get('title') | |
content = request.POST.get('content') | |
Post.objects.create(title=title, content=content, author=request.user) | |
return redirect('post_list') | |
class LoginView(View): | |
def get(self, request): | |
return render(request, 'login.html') | |
def post(self, request): | |
username = request.POST.get('username') | |
password = request.POST.get('password') | |
user = authenticate(request, username=username, password=password) | |
if user is not None: | |
login(request, user) | |
return redirect('home') | |
return render(request, 'login.html', {'error': 'Invalid credentials'}) | |
class LogoutView(View): | |
def post(self, request): | |
logout(request) | |
return redirect('login') | |
# urls.py | |
from django.urls import path | |
from . import views | |
urlpatterns = [ | |
path('users/', views.UserViewSet.as_view(), name='user_list'), | |
path('posts/', views.PostViewSet.as_view(), name='post_list'), | |
path('login/', views.LoginView.as_view(), name='login'), | |
path('logout/', views.LogoutView.as_view(), name='logout'), | |
] | |
INSTALLED_APPS = [ | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
] | |
MIDDLEWARE = [ | |
'django.middleware.security.SecurityMiddleware', | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.middleware.common.CommonMiddleware', | |
'django.middleware.csrf.CsrfViewMiddleware', | |
'django.contrib.auth.middleware.AuthenticationMiddleware', | |
'django.contrib.messages.middleware.MessageMiddleware', | |
'django.middleware.clickjacking.XFrameOptionsMiddleware', | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment