Forms.py notworking

Hello Guys,
I am building a blog application using django and bootstrap (extends base.html) as well as using class based views. As you can see in the image the bootstrap is not taking effect on the add post in the image. As well as in forms.py ‘category’: forms.Select(attrs={‘class’:‘form-control’}) but it is still showing as text box not a drop down box. I am not able to understand what I am missing. could anyone please help me understand what is the issue
Thank you.

forms.py

from django import forms
from .models import Post

class PostForms(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'author','category', 'body')

        Widgets = {
            'title' : forms.TextInput(attrs={'class':'form-control'}),
            'author' : forms.Select(attrs={'class':'form-control'}),
            'body': forms.Textarea(attrs={'class': 'form-control'}),
            'category': forms.Select(attrs={'class':'form-control'}),   
        }

class EditForms(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'author','category', 'body')

        Widgets = {
            'title' : forms.TextInput(attrs={'class':'form-control'}),
            'author' : forms.Select(attrs={'class':'form-control'}),
            'body': forms.Textarea(attrs={'class': 'form-control'}),
            'category': forms.Select(attrs={'class':'form-control'}),
        }

views.py

from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, TemplateView, CreateView, UpdateView, DeleteView
from cavertech.models import Post
from cavertech.forms import PostForms,EditForms

# Create your views here.
#def home(request):
#    return render(request, 'home1.html',{})

class Home_view(TemplateView):    # Hoeme page view 
    template_name = 'home.html'

class Blog_view(ListView):       # Blog list view  
    model = Post
    template_name = 'blog_list.html'
    ordering = ['-date']

class Article_view(DetailView):  # Blog detail view 
    model = Post
    template_name = 'article_view.html'

class Create_post_view(CreateView): # Create Blog
    model = Post
    form_class = PostForms
    template_name = 'add_post.html'
    #fields = '__all__'

class Update_post_view(UpdateView): # update Blog
    model = Post
    form_class = EditForms
    template_name = 'update_post.html'
    #fields = ['title', 'author', 'body']

class Delete_post_view(DeleteView): 
    model = Post
    template_name = 'delete_post.html'
    success_url = reverse_lazy('blogview')

add-post.html

{% extends 'base.html' %}
{% block title %}Create Post{% endblock %}

{% block content %}
{% if user.is_authenticated %}
<h1>Create Post..</h1>
<br/><br/>
<div class="form-group">
    <form method="post">{% csrf_token %}
        {{ form.as_p }}
        <button class = "btn btn-secondary"> Post </button>
 
</div>
{% endif %}
   
{% endblock %}

What does your Post model look like?

from django.db import models 
from django.contrib.auth.models import User
from django.urls import reverse


# Create your models here.

# Category database
class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name 


# Blog Post database 
class Post(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField()
    date = models.DateField(auto_now_add=True)
    meta_tags = models.CharField(max_length=500)
    category = models.CharField(max_length=255)

    def __str__(self):
        return self.title + ' | ' + str(self.author)

    def get_absolute_url (self):
        return reverse('articleview', args=(str(self.id)))

# Email collection 
class Contact(models.Model):
    email = models.EmailField()

    def __str__(self):
        return self.email

Hello @KenWhitesell thank you for guiding at the right direction, I figured the choice issue by adding the following code. As I update the category class model with new data it don’t update dynamic, How do I active that?

category = models.CharField(blank=True, choices=Category.objects.all().values_list('name','name'), max_length=100)

but I am still not able to get the bootstrap working.

Once the Form is initialized new models that would be in the Queryset will not be taken into account. You will have to reinitialize the form.

How do I do that ? In form.py with for loop for category?

Actually, your category field in your model should be a ForeignKey to Category.

Copying from the docs at choices:

Note that choices can be any sequence object – not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you’re probably better off using a proper database table with a ForeignKey . choices is meant for static data that doesn’t change much, if ever.

Do you mean like this ?
category = models.ForeignKey(Category, on_delete=models.CASCADE)

I have designed a HTML/CSS page for creating blog post how do I link it to django? Is it via forms?

Correct.

It’s a mix of template work and forms. You can convert that page to a template, and use the form fields to supply the data-entry elements.

I am trying the bootstrap as passing the form but its not working. Why is that?

We need to see the template(s) involved.

  • Are you including the references to the bootstrap css / js files in those templates?
  • Are you seeing them being requested by the browser?
  • Are they being returned by the server?
  • Are you seeing the bootstrap classes rendered in the template in the browser on those fields?

I have created a base.html and I am passing

{% extends 'base.html' %}
{% block content %}

See Creating forms from models | Django documentation | Django.

You’ve got Widgets in your Meta class instead of widgets.

1 Like

Thank you for all you help. I would also like to know is it possible in django to add MFA to the login page.

Whenever I’m looking for a django package that does something, I generally start with searching on djangopackages.org.
In this case, searching for MFA will show you a couple of different options for doing this.
Even if you don’t want to use one of those packages, you can at least look at them to get some ideas.

Hello I am trying to get the current login user as I would like to have it as my author name in the blog post. Right now i am using js to get a user id.

add_post.html

<script>
    var name = "{{user.id}}";
    document.getElementById("current_user").value = name;
</script>

forms.py


class PostForms(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'author', 'category','snippet', 'body')

        widgets = {
            'title' : forms.TextInput(attrs={'class':'form-control'}),
            'author' : forms.TextInput(attrs={'class':'form-control', 'value': '','id':'current_user','type':'hidden'}),

Is their a better way to achieve this ?