Skip to content

Commit

Permalink
Item Model Form and Create View
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Jan 10, 2024
1 parent b5fd020 commit 8068e29
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/cfehome/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

from landing import views as landing_views
from projects import views as projects_views

urlpatterns = [
path("", landing_views.home_page_view),
path("items/", include('items.urls')),
path("about/", landing_views.about_page_view),
path("activate/project/<slug:handle>/",
projects_views.activate_project_view),
path("deactivate/project/<slug:handle>/",
path("deactivate/project/<slug:handle>/",
projects_views.deactivate_project_view),
path("err", landing_views.server_error_page),
path("admin/", admin.site.urls),
Expand Down
4 changes: 4 additions & 0 deletions src/items/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.contrib import admin

# Register your models here.
from .models import Item


admin.site.register(Item)
9 changes: 9 additions & 0 deletions src/items/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms

from .models import Item


class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ['title', 'description']
25 changes: 25 additions & 0 deletions src/items/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
URL configuration for cfehome project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
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.urls import path

from . import views

app_name='items'
urlpatterns = [
path("create/", views.item_create_view, name='create')
]
21 changes: 20 additions & 1 deletion src/items/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render

# Create your views here.
from .forms import ItemForm
from .models import Item

# @project_required
@login_required
def item_create_view(request):
if not request.project.is_activated:
return render(request, 'projects/activate.html', {})
form = ItemForm(request.POST or None)
if form.is_valid():
item_obj = form.save(commit=False)
item_obj.project = request.project
item_obj.added_by = request.user
item_obj.save()
form = ItemForm()
context = {
"form": form
}
return render(request, 'items/create.html', context)
16 changes: 16 additions & 0 deletions src/templates/items/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'base.html' %}


{% block content %}

<h1>Create new item for {{ request.project.title }}</h1>
<form method="post" action="">{% csrf_token %}

{{ form.as_p }}

<button type="submit">Create</button>

</form>


{% endblock content %}

0 comments on commit 8068e29

Please sign in to comment.