Created Project model

This commit is contained in:
Kristofers Solo 2023-03-27 20:48:27 +03:00
parent 6dc7acbd7c
commit 7dd314b08c
6 changed files with 61 additions and 4 deletions

View File

@ -0,0 +1,9 @@
from django import forms
from .models import Project
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ["title", "description"]

View File

@ -1,3 +1,15 @@
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class Project(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
description = models.TextField()
create_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.repo_name
def get_absolute_url(self):
return f"/projects/{self.id}"

View File

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index")
]

View File

@ -1,3 +1,25 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required, permission_required
from django.shortcuts import redirect, render
# Create your views here.
from .forms import ProjectForm
from .models import Project
def index(request):
projects = Project.objects.all()
return render(request, "fossdb/index.html", {"title": "FOSSDB", "projects": projects})
@login_required(login_url="account/login/")
@permission_required("fossdb.add_post)", login_url="account/login/", raise_exception=True)
def create_project(request):
if request.method == "POST":
project = ProjectForm(request.POST)
if project.is_valid():
post = project.save(commit=False)
post.author = request.user
post.save()
return redirect("")
else:
project = ProjectForm()
return render(request, "main/create_project.html", {"title": "Add project", "form": project})

View File

@ -37,6 +37,7 @@ ALLOWED_HOSTS = config["ALLOWED_HOSTS"]
# Application definition
INSTALLED_APPS = [
"fossdb",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",

View File

@ -13,9 +13,15 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.urls import include, path
urlpatterns = [
path("", include("fossdb.urls")),
path("admin/", admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)