St JC
St JC

Reputation: 1

generic createview and django form only rendering a button in a template

I have encountered a funny challenge, as am building some apps where they want track some expenses and revenues profits ... I used generic views to create and get details and delete, all work fine with expenses, but I copied the same code and I used it for revenues from expenses you can't imagine this view is rendering a button only yet it has a form, NOTE SAME CODE WORK WELL ON EXPENSES. I even exchanged templates but it kept rendering the former content even after changing the template. it doesn't matter what's in the template the view just renders the same thing even when I remove the form, even when I put pass.

class AccountsRevenueCreate(CreateView):
template_name='dashboard/expense_create.html'
model= AccountsExpense
success_url = reverse_lazy('dashboard:expenses')
form_class=AccountsExpenseForm

and the form

class AccountsRevenueForm(forms.ModelForm):
class Meta:
    model = AccountsRevenue
    fields = '__all__'

and in the template

<div class="row">
                    <div class="col-12">
                       <h5 class="form-title"><span>revenue Update 
                     </span></h5>
                    </div>
                    <form method="post", action="{% url 
                    'dashboard:revenue_add' %}">
                    {% csrf_token %}
                       
                     {% comment %} {{ form|crispy }} {% endcomment %}     
                     {{ form.as_p }} 

                    <button type="submit" class="btn btn-primary col-12 col-sm-12">Save 
                    </button>
                   </form>
                 </div>

and the URLs

path ('accounts/revenues/create/', AccountsRevenueCreate.as_view(), name='revenue_create'),

I have chosen to show you only where the problem is not paste unnecessary code

here are the models you requested me to update

from django.db import models

class AccountsRevenue(models.Model):
revenue_of = models.CharField(max_length=250, default='',null=False)
revenue_by = models.CharField(max_length=250, default='',null=False)
amount = models.IntegerField( default='100,000') 
day_on_which = models.DateField(null=True, blank=True)
evidence_document = models.ImageField(upload_to = 'uploads/', blank=True, default='')
signature = models.ImageField(upload_to = 'uploads/', blank=True, default='')


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

def get_absolute_url(self):
    return reverse('dashboard:revenue', kwargs= {'pk':self.pk} )

def get_document(self):
    if self.evidence_document and hasattr(self.evidence_document, 'url'):
        return self.evidence_document.url
    else:
        return "/static/assets/img/user.png"




 class AccountsExpense(models.Model):
expense_of= models.CharField(max_length=250, default='',null=False)
expense_by = models.CharField(max_length=250, default='',null=False)
amount = models.IntegerField( default='100,000') 
day_on_which = models.DateField(null=True, blank=True)
evidence_document = models.ImageField(upload_to = 'uploads/', 
blank=True, default='')
signature = models.ImageField(upload_to = 'uploads/', blank=True, 
 default='')


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

def get_absolute_url(self):
    return reverse('dashboard:expense', kwargs= {'pk':self.pk} )

def get_document(self):
    if self.evidence_document and hasattr(self.evidence_document, 
    'url'):
        return self.evidence_document.url
    else:
        return "/static/assets/img/user.png"







from django.urls import URLPattern, path
from django.conf.urls.static import static
from. views import profiles, Profile_detail, Profile_update, 
Profile_delete, dashboard, Profile_create,\
AccountsRevenues, revenue_detail_view, AccountsRevenueUpdate, 
AccountsRevenue_delete, \
AccountsExpenses, AccountsExpenseDetail, AccountsExpenseCreate, 
AccountsExpenseUpdate, AccountsExpenseDelete,\
        userprofileupdate, revenue_add, AccountsRevenueCreate
         


app_name = "dashboard"
urlpatterns = [

path ('accounts/revenues', AccountsRevenues.as_view(), name='revenues'),
path ('accounts/revenues/<str:pk>/', revenue_detail_view, 
name='revenue'),
path ('accounts/revenues/<str:pk>/update/', 
AccountsRevenueUpdate.as_view(), name='revenue_update'),
path ('accounts/revenues/<str:pk>/delete/', 
AccountsRevenue_delete.as_view(), name='revenue_delete'),
path ('accounts/revenues/create/', AccountsRevenueCreate.as_view(), 
name='revenue_create'),


# accounts expenses 
path ('accounts/expenses', AccountsExpenses.as_view(), name='expenses'),
path ('accounts/expenses/<str:pk>', AccountsExpenseDetail.as_view(), 
name='expense'),
path ('accounts/expenses/<str:pk>/update/', 
AccountsExpenseUpdate.as_view(), name='expense_update'),
path ('accounts/expenses/<str:pk>/delete/', 
 AccountsExpenseDelete.as_view(), name='expense_delete'),
path ('accounts/expenses/create/', AccountsExpenseCreate.as_view(), 
name='expense_create'),

]

I have added a screenshot of what it comes out as enter image description here

Upvotes: 0

Views: 343

Answers (1)

inquirer
inquirer

Reputation: 4823

left model.py, forms.py, urls.py as it was.

views.py in the line: template_name = 'bboard/expense_create.html' replace bboard with the name of the folder where the templates are located. I have it templates/bboard. Also in your field: form_class=AccountsExpenseForm changed to form_class = AccountsRevenueForm. And the return goes to the same page with the form: success_url = reverse_lazy('revenue_create').

success_url can then be returned to what it was, now you need to make sure that everything works.

views.py

class AccountsRevenueCreate(CreateView):
    template_name = 'bboard/expense_create.html'
    model = AccountsExpense
    success_url = reverse_lazy('revenue_create')
    form_class = AccountsRevenueForm

expense_create.html in the template you have an error form method="post", there should not be a comma.

<div class="row">
    <div class="col-12">
        <h5 class="form-title"><span>revenue Update
                     </span></h5>
    </div>
    <form method="post" action="{% url 'revenue_create' %}">
        {% csrf_token %}

        {% comment %} {{ form|crispy }} {% endcomment %}
        {{ form.as_p }}

        <button type="submit" class="btn btn-primary col-12 col-sm-12">Save
        </button>
    </form>
</div>

Followed the link: http://localhost:8000/accounts/revenues/create/

Screenshot attached: enter image description here

Upvotes: 1

Related Questions