As a test run after reading through Django for Beginners, I am trying to make a server with custom D&D character sheets. Here is my the relevant fields in my model (keep in mind that I have other fields, I just didn't list them for simplicity):
from django.db import models
from django.urls import reverse
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as gtl
import json
class charSheet(models.Model):
# character's name
char_name = models.CharField(max_length=45)
# player's name which is the key that will
# group together different character sheets
# together
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
Here is my forms.py:
from django import forms
from .models import charSheet
class CharacterForm(forms.ModelForm):
class Meta:
model = charSheet
# fields = [field.name for field in charSheet._meta.get_fields() if field.name != "author"]
# fields = "__all__"
fields = ["char_name", "author"]
and the relevant part of my views.py
class CharacterCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
model = charSheet
template_name = "character_new.html"
fields = [
field.name for field in charSheet._meta.get_fields() if field.name != "author"
]
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
obj = self.get_object()
return obj.author == self.request.user
Here is my urls.py
rom django.urls import path
from .views import (
CharacterListView,
CharacterDetailView,
CharacterCreateView,
CharacterUpdateView,
CharacterDeleteView,
CharacterDetailTestingView,
)
urlpatterns = [
path("", CharacterListView.as_view(), name="home"),
path(
"<str:author>/character/<int:pk>/",
CharacterDetailView.as_view(),
name="character_details",
),
path(
"character/new/",
CharacterCreateView.as_view(),
name="character_new",
),
path(
"<str:author>/character/<int:pk>/edit",
CharacterUpdateView.as_view(),
name="character_update",
),
path(
"<str:author>/character/<int:pk>/delete",
CharacterDeleteView.as_view(),
name="character_delete",
),
path(
"<str:author>/character/<int:pk>/testing",
CharacterDetailTestingView.as_view(),
name="character_details_testing",
),
]
Last but not least, my incredibly complicated html template:
{% extends "base.html" %} {% block content %} {% load crispy_forms_tags %}
<div class="character-entry">
<form method="post"> {% csrf_token %}
<div class="form-group col-md-6 mb-0">
hello {{ charsheet.author }}
<label for="{{ form.char_name.id_for_label }}"> character name</label>
{{ form.char_name }}
{% if form.char_name.errors %}
<div class="invalid-feedback">{{ form.char_name.errors | as_crispy_field }}</div>
{% endif %}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<p>
<a href="{% url 'character_update' author=charsheet.author pk=charsheet.pk %}">+ Update Character</a>
</p>
<p>
<a
href="{% url 'character_delete' author=charsheet.author pk=charsheet.pk %}"
>
+ Delete Character</a
>
</p>
<p>
<a
href="{% url 'character_details_testing' author=charsheet.author pk=charsheet.pk %}"
>
+ testing html</a>
</p>
{% endblock content %}
I'm not sure why, because it posts through and I get a code 200 meaning it got the form, but my database is not updating. What should I do to fix this or at least diagnose this?
I tried writing print statements to see what gets called, and it seems everything is going through. I'm trying to figure out how to use HttpResponse, and I tried implementing in the view a get
and post
method, but I think I'm just doing them wrong. Thanks in advance!
It's not redirecting, which normally means the form wasn't valid. If so your answer will be somewhere in form.errors
. I would suggest either
Temporarily replace your complicated HTML by a simple {{form.as_p}}
to see what it says (I'm guessing at a non-field-error), or
Add print( form.errors)
to your view. You'll have to subclass form_invalid
to do this
def form_invalid( self, form):
print( form.errors ) # for debugging
return super().form_invalid( form)