multiple changes related to crispy-forms
This commit is contained in:
parent
ce5a0387ba
commit
65bef6f3af
|
@ -8,12 +8,7 @@
|
|||
<h5 class="modal-title">{{ modal_title }}</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
{{ form|crispy }}
|
||||
<button type="submit" class="btn btn-primary" hx-post="{% url 'htmx_create_customer' %}" hx-target="#htmx-modal-position">Save</button>
|
||||
<button type="button" class="btn btn-secondary" onclick="closeModal()">Close</button>
|
||||
</form>
|
||||
{% crispy form %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
import floppyforms.__future__ as forms
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Field, Button, Div
|
||||
from crispy_forms.bootstrap import FormActions, Modal
|
||||
|
||||
from core import utils
|
||||
|
||||
|
@ -6,12 +9,38 @@ from customers.models import Customer
|
|||
from customers.models import Location
|
||||
|
||||
from devices.models import Device
|
||||
from devices.models import DeviceCategory
|
||||
from devices.models import DeviceInNet
|
||||
from devices.models import Warranty
|
||||
|
||||
from users.models import User
|
||||
|
||||
|
||||
class DeviceCategoryForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = DeviceCategory
|
||||
fields = (
|
||||
'name',
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DeviceCategoryForm, self).__init__(*args, **kwargs)
|
||||
|
||||
self.helper = FormHelper()
|
||||
self.helper.layout = Layout(
|
||||
Modal(
|
||||
'name',
|
||||
# Div('field2'),
|
||||
title="This is my modal"),
|
||||
FormActions(
|
||||
Submit(
|
||||
'save', 'Save', hx_post="{% url 'htmx_create_customer' %}",
|
||||
hx_target="#htmx-modal-position"),
|
||||
Button('cancel', 'Cancel', css_class="btn btn-secondary",
|
||||
onclick="closeModal()"))
|
||||
)
|
||||
|
||||
|
||||
class DeviceCreateForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Device
|
||||
|
@ -47,6 +76,27 @@ class DeviceUpdateForm(forms.ModelForm):
|
|||
self.fields['customer'].queryset = customers
|
||||
self.fields['location'].queryset = locations
|
||||
self.fields['user'].queryset = users
|
||||
self.helper = FormHelper()
|
||||
self.helper.layout = Layout(
|
||||
Fieldset(
|
||||
'',
|
||||
'name',
|
||||
Field('customer'),
|
||||
'location',
|
||||
'user',
|
||||
'category',
|
||||
HTML("""
|
||||
<a hx-get="{% url 'device_category_create' %}" hx-target="#htmx-modal-position" href="" class="add" title="Add" data-toggle="tooltip"><i class="material-icons">add</i></a>
|
||||
"""),
|
||||
'serialnumber',
|
||||
'description',
|
||||
),
|
||||
FormActions(
|
||||
Submit('save', 'Save'),
|
||||
HTML(
|
||||
"""<a href="{{ request.META.HTTP_REFERER }}" class="btn btn-secondary">Cancel</a>""")
|
||||
)
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Device
|
||||
|
|
|
@ -4,24 +4,19 @@
|
|||
{% block section_title %}Edit Device{% endblock %}
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
<div class="row">
|
||||
{% csrf_token %}
|
||||
<div class="col">
|
||||
<div class="card mt-3">
|
||||
<div class="card-body">
|
||||
{{ form|crispy }}
|
||||
<div class="row">
|
||||
{% csrf_token %}
|
||||
<div class="col">
|
||||
<div class="card mt-3">
|
||||
<div class="card-body">
|
||||
{% crispy form %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<br>
|
||||
<input type="submit" value="Save" class="btn btn-primary">
|
||||
<a href="{{ request.META.HTTP_REFERER }}" class="btn btn-primary">Cancel</a>
|
||||
<input type="hidden" id="previous_page" name="previous_page" value="/previous/page/url">
|
||||
</form>
|
||||
<script>
|
||||
prev = document.getElementById("previous_page");
|
||||
prev.value = document.referrer;
|
||||
</script>
|
||||
<br>
|
||||
|
||||
</form>
|
||||
<div class="col" id="htmx-modal-position"></div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -35,4 +35,6 @@ urlpatterns = [
|
|||
views.DeviceInNetDeleteView.as_view(),
|
||||
name='device_in_net_delete'),
|
||||
path('warranties/', views.warranties_view, name='warranties'),
|
||||
path('create/devices/category', views.htmx_create_device_cagetory,
|
||||
name='device_category_create')
|
||||
]
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http.response import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.shortcuts import render
|
||||
from django.template.response import TemplateResponse
|
||||
|
@ -17,6 +18,7 @@ from core import utils
|
|||
|
||||
from .decorators import device_view_permission
|
||||
|
||||
from .forms import DeviceCategoryForm
|
||||
from .forms import DeviceCreateForm
|
||||
from .forms import DeviceInNetCreateForm
|
||||
from .forms import DeviceInNetUpdateForm
|
||||
|
@ -102,7 +104,7 @@ def device_update_view(request, pk):
|
|||
"""
|
||||
A view to create a customer.
|
||||
"""
|
||||
template_name = 'computers/computer_update.html'
|
||||
template_name = 'devices/device_update.html'
|
||||
device = utils.get_object_with_view_permission(Device,
|
||||
user=request.user,
|
||||
pk=pk)
|
||||
|
@ -198,3 +200,22 @@ class DeviceInNetDeleteView(LoginRequiredMixin, DeleteView):
|
|||
class DeviceManufacturerDetailView(LoginRequiredMixin, DetailView):
|
||||
model = DeviceManufacturer
|
||||
template_name = 'devices/manufacturer_details.html'
|
||||
|
||||
|
||||
@login_required
|
||||
def htmx_create_device_cagetory(request):
|
||||
template_path = "devices/partials/device_category_create.html"
|
||||
if request.method == "POST":
|
||||
form = DeviceCategoryForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save(commit=True)
|
||||
return HttpResponse("<script>location.reload()</script>")
|
||||
else:
|
||||
return TemplateResponse(request,
|
||||
template_path,
|
||||
context={"form": form})
|
||||
form = DeviceCategoryForm()
|
||||
context = {"form": form}
|
||||
return TemplateResponse(request,
|
||||
template_path,
|
||||
context)
|
||||
|
|
|
@ -14,6 +14,7 @@ CSRF_TRUSTED_ORIGINS = [
|
|||
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
|
||||
|
||||
DEBUG = os.environ.get('DJANGO_DEBUG')
|
||||
CRISPY_FAIL_SILENTLY = not DEBUG
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
|
|
|
@ -10,6 +10,7 @@ SECRET_KEY = 'development_key'
|
|||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
CRISPY_FAIL_SILENTLY = not DEBUG
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
|
|
|
@ -13,6 +13,7 @@ CSRF_TRUSTED_ORIGINS = [
|
|||
SECRET_KEY = "foo"
|
||||
|
||||
DEBUG = True
|
||||
CRISPY_FAIL_SILENTLY = not DEBUG
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
|
|
|
@ -9,6 +9,7 @@ SECRET_KEY = os.getenv('SECRET_KEY')
|
|||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = False
|
||||
CRISPY_FAIL_SILENTLY = not DEBUG
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
|
|
Loading…
Reference in New Issue