3

So, this is my first project with Django, and for the first time I am using the Django-Tables2. I have a Django table with three columns, last column contains add button for each row which when clicked takes me to another page where I can select options. on submitting the options, the mapping gets saved in database and user gets redirected to the tables page again. Here I want to change the "add" button to "Done".


def edit_item(request, pk):
    item = get_object_or_404(BAReg, id=pk)


    if request.method == "POST":
        form = ProcessForm(request.POST or None, instance=item)

        if form.is_valid():
            processes = form.cleaned_data.get('process')

            print(processes)
            for country in processes:
                    issue = ProcessReg()
                    issue.ba = item.ba
                    issue.regulation = item.regulation
                    issue.process = country
                    issue.save()
        return redirect('secondpage')

    else:
        form = ProcessForm(request.POST,instance=item)




    return render(request, 'addbutton.html', {'form': form}) 

  def secondpage(request):
        ba = request.session.get('ba')
        print(ba)
        table = PersonTable(BAReg.objects.filter(regulation=ba))

        RequestConfig(request).configure(table)
        context = {'table': table}
        return render(request,'secondpage.html/',context)


class PersonTable(tables.Table):
   Process =  tables.LinkColumn('edit_item',text='Add Process', args=[A('pk')])
   class Meta:
       model = BAReg
       template_name = 'django_tables2/bootstrap4.html'



<form method='post' action=''>
      {% csrf_token %}
      {{ form.as_p }}

      <input type='submit' value='submit'>


  </form>

{% load render_table from django_tables2 %}
{% block content %}
<!-- <form  method="POST"> -->

    {% render_table table %}
    <!-- {{ form.as_p }} -->
<!-- <button type="submit" style="margin-left: 680px">Submit</button>
</form> -->


{% endblock %}


urlpatterns = [
    path('', views.countries_view, name='index'),
    path('<int:pk>/', views.edit_item, name="edit_item"),
    path('table/',views.secondpage, name ="secondpage"),
]

1 Answers1

0

So, I found the solution

class CustomTemplateColumn(tables.TemplateColumn):
   def render(self, record, table, value, bound_column, **kwargs):
        if record.status == "done":
            return 'DONE'

        return super(CustomTemplateColumn, self).render(record, table, value, bound_column, **kwargs)

class CustomTemplateColumnEdit(tables.TemplateColumn):
   def render(self, record, table, value, bound_column, **kwargs):
        if record.edit_status == "done":
            return 'DONE'

        return super(CustomTemplateColumnEdit, self).render(record, table, value, bound_column, **kwargs)




class PersonTable(tables.Table):
    T1 = '<a href="edit/businessactivity/{{record.id}}/">EDIT</a>'
    T2 = '<a href="edit/{{ record.id }}">ADD</a>'
    Edit_Regulation = CustomTemplateColumnEdit(T1)
    Process   = CustomTemplateColumn(T2)
    class Meta:
        model = BAReg
        fields =['businessdefinition_a', 'regulation']
        template_name = 'django_tables2/semantic.html'