0

I have a simple app which asks a user if they have completed their hour task (such as getting up from the desk and streching)

however, my form is not rendering and I'm non the wiser to why after debugging for a good while.

models.py

from django.db import models
from django.contrib.auth import get_user_model


class StrechesTracker(models.Model):

    id = models.AutoField(
        primary_key=True,
        editable=False
    )

    excercise = models.BooleanField(
        choices=(
            (True,'Yes'), (False,'No')
        )
    )

    created_at = models.DateTimeField(auto_now_add=True)

    created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True)

    def __str__(self) -> str:
        return self.excercise

forms.py

from django import forms
from .models import StrechesTracker

class StrechForm(forms.ModelForm):

    class Meta:
        model = StrechesTracker
        fields = '__all__'

views.py

from typing import Any, Dict

from django.views.generic import CreateView

from .models import StrechesTracker
from .forms import StrechForm


class TrackerView(CreateView):

    model = StrechesTracker
    form_class  = StrechForm
    template_name = 'home.html'

    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:

        context = super().get_context_data(**kwargs)

        return context

    def form_valid(self, form: StrechForm):
        return super().form_valid(form)
    

home.html

{% extends "_base.html" %} {% load crispy_forms_tags %} {% block content %}
<div class="jumbotron">
  <h1>Done Hourly Streches?</h1>
<form action="" method="post">
  {% csrf_token %} {{ form|crispy }}
  <button class="btn btn-success" type="submit">Yes</button>
</form>
</div>

{% endblock content %}

Additional info.

unsure if this is required but this is the table definition for my model


CREATE TABLE public.tracker_strechestracker
(
    id integer NOT NULL DEFAULT nextval('tracker_strechestracker_id_seq'::regclass),
    excercise boolean NOT NULL,
    created_at timestamp with time zone NOT NULL,
    created_by_id bigint
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.tracker_strechestracker
    OWNER to postgres;



screen grab of page.

enter image description here

enter image description here

1 Answer 1

2

i have redone your project and tested it, the code below is working

Updated answer:

Models:

class StrechesTracker(models.Model):

    excercise = models.BooleanField(choices=((True,'Yes'), (False,'No')))
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    created_at = models.DateTimeField(auto_now_add=True)   

    def __str__(self):
        return str(self.excercise)

Forms

class StrechForm(forms.ModelForm):

    class Meta:
        model = StrechesTracker
        fields = ['excercise',]# you can go back to to see users '__all__'

Views:

class TrackerView(CreateView):

    model = StrechesTracker
    form_class  = StrechForm
    template_name = 'home.html'
    success_url = "/track/" # any url to redirect after from submit

    def form_valid(self, form: StrechForm):
    form.instance.created_by = self.request.user # added this line so that the authenticated user is the one filling form,you can comment this line to go back to to see users 
    return super(TrackerView, self).form_valid(form)

URLS:

path('track/', TrackerView.as_view(), name='TrackerView'),

Templates:

<div class="jumbotron">
    <h1>Done Hourly Streches?</h1>
<form action="" method="post">
    {% csrf_token %} {{ form }}
    <button class="btn btn-success" type="submit">Send</button>
</form>
#add crispy after you try if this works for you
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply, however I'm still getting nothing back, just re-build the docker container to be sure and no change.
When you say "form is not rendering", what actually is happening?
@michjnich see updated question basically I'm expecting all the fields to appear as input fields - but nothing is coming through.
alright i have updated the answer, hope that works for you, good luck
Thanks Taha I will test shortly and get back to you really appreciate your help !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.