Site icon JnPnote

Following Django Tutorial (CRM)-Part14

This post is for myself to remember how to build Django project.
I did follow the tutorial from the JustDjango Learn website free tutorial.
And this is the fourth tutorial named Getting Started With Django.
I am not going over all the details and descriptions for each part. There are good explanations on video of the JustDjango Learn. So, visit their site and try their tutorials if you need more details. Also, the orders of this post and their video might be different because I put things first what I think should come first.

I am doing this on Windows 10 with just Windows PowerShell. Not using virtual machines at all.

1. Categorize Model

2. Category List View

class CategoryListView(LoginRequiredMixin, generic.ListView):
	template_name = "category_list.html"
	context_object_name = "category_list"

	def get_context_data(self, **kwargs):
		context = super(CategoryListView, self).get_context_data(**kwargs)
		user = self.request.user

		if user.is_organisor:
			queryset = Lead.objects.filter(
				organisation=user.userprofile
			)
		else:
			queryset = Lead.objects.filter(
				organisation=user.agent.organisation
			)
		return context

		context.update({
			"unassigned_lead_count": queryset.filter(category__isnull=True).count()
		})


	def get_queryset(self):
		user = self.request.user
		if user.is_organisor:
			queryset = Category.objects.filter(
				organisation=user.userprofile
			)
		else:
			queryset = Category.objects.filter(
				organisation=user.agent.organisation
			)
		return queryset
{% extends "base.html" %}

{% block content %}

<section class="text-gray-600 body-font">
  <div class="container px-5 py-24 mx-auto">
    <div class="flex flex-col text-center w-full mb-20">
      <h1 class="sm:text-4xl text-3xl font-medium title-font mb-2 text-gray-900">Category</h1>
      <p class="lg:w-2/3 mx-auto leading-relaxed text-base">
		  These categories segment the leads
	  </p>
    </div>
    <div class="lg:w-2/3 w-full mx-auto overflow-auto">
      <table class="table-auto w-full text-left whitespace-no-wrap">
        <thead>
          <tr>
            <th class="px-4 py-3 title-font tracking-wider font-medium text-gray-900 text-sm bg-gray-100 rounded-tl rounded-bl">Name</th>
            <th class="px-4 py-3 title-font tracking-wider font-medium text-gray-900 text-sm bg-gray-100">Lead Count</th>
          </tr>
        </thead>
        <tbody>
			<tr>
				<td class="px-4 py-3">Unassigned</td>
				<td class="px-4 py-3">{{ unassigned_lead_count }}</td>
			</tr>
			{% for category in category_list %}
				<tr>
					<td class="px-4 py-3">{{ category.name }}</td>
					<td class="px-4 py-3">todo count</td>
				</tr>
		  	{% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</section>
GitHub

{% endblock content %}

3. category detail view

class CategoryDetailView(LoginRequiredMixin, generic.DetailView):
	template_name = "category_detail.html"
	context_object_name = "category"


	def get_queryset(self):
		user = self.request.user
		if user.is_organisor:
			queryset = Category.objects.filter(
				organisation=user.userprofile
				)
		else:
			queryset = Category.objects.filter(
				organisation=user.agent.organisation
				)
		return queryset
{% extends "base.html" %}

{% block content %}

<section class="text-gray-600 body-font">
  <div class="container px-5 py-24 mx-auto">
    <div class="flex flex-col text-center w-full mb-20">
      <h1 class="sm:text-4xl text-3xl font-medium title-font mb-2 text-gray-900">{{ category.name }}</h1>
      <p class="lg:w-2/3 mx-auto leading-relaxed text-base">
		  These are the leads under this category
	  </p>
    </div>
    <div class="lg:w-2/3 w-full mx-auto overflow-auto">
      <table class="table-auto w-full text-left whitespace-no-wrap">
        <thead>
          <tr>
            <th class="px-4 py-3 title-font tracking-wider font-medium text-gray-900 text-sm bg-gray-100 rounded-tl rounded-bl">First Name</th>
            <th class="px-4 py-3 title-font tracking-wider font-medium text-gray-900 text-sm bg-gray-100">Last Name</th>
          </tr>
        </thead>
        <tbody>
			{% for lead in category.leads.all %}
				<tr>
					<td class="px-4 py-3">{{ lead.first_name }}</td>
					<td class="px-4 py-3">{{ lead.last_name }}</td>
				</tr>
		  	{% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</section>

{% endblock content %}

4. category update view

class LeadCategoryUpdateForm(forms.ModelForm):
	class Meta:
		model = Lead
		fields = (
			'category',
		)
class LeadCategoryUpdateView(LoginRequiredMixin, generic.UpdateView):
	template_name = "lead_category_update.html"
	form_class = LeadCategoryUpdateForm


	def get_queryset(self):
		user = self.request.user
		if user.is_organisor:
			queryset = Lead.objects.filter(
				organisation=user.userprofile
				)
		else:
			queryset = Lead.objects.filter(
				organisation=user.agent.organisation
				)
			queryset = queryset.filter(agent__user=user)
		return queryset

	def get_success_url(self):
		return reverse("leads:lead-detail", kwargs={"pk": self.get_object().id})
{% extends "base.html" %}

{% block content %}

<section class="text-gray-600 body-font overflow-hidden">
	<div class="container px-5 py-24 mx-auto">
		<div class="lg:w-4/5 mx-auto flex flex-wrap">
			<div class="lg:w-1/2 w-full lg:pr-10 lg:py-6 mb-6 lg:mb-0">
				<h2 class="text-sm title-font text-gray-500 tracking-widest">
					LEAD
				</h2>
				<h1 class="text-gray-900 text-3xl title-font font-medium mb-4">
					{{ lead.first_name }} {{ lead.last_name }}
				</h1>
				<div class="flex mb-4">
					<a href="{% url 'leads:lead-detail' lead.pk %}" class="flex-grow border-b-2 border-gray-300 py-2 text-lg px-1">
						Description
					</a>
					<a href="{% url 'leads:lead-category-update' lead.pk %}" class="flex-grow text-indigo-500 border-b-2 border-indigo-500 py-2 text-lg px-1">
						Category
					</a>
					<a href="{% url 'leads:lead-update' lead.pk %}" class="flex-grow border-b-2 border-gray-300 py-2 text-lg px-1">
						Update Details
					</a>
				</div>
				<form method="post">
					{% csrf_token %}
					{{ form.as_p }}
					<button type="submit">Submit</button>
				</form>
				<a href="{% url 'leads:lead-delete' lead.pk %}" class="w-1/2 mt-3 flex ml-auto text-white bg-indigo-500 border-0 py-2 px-6 focus:outline-none hover:bg-indigo-600 rounded">
					Delete
				</a>
			</div>
			<img alt="ecommerce" class="lg:w-1/2 w-full lg:h-auto h-64 object-cover object-center rounded" src="https://dummyimage.com/400x400">
		</div>
	</div>
</section>
{% endblock content %}

Exit mobile version