Site icon JnPnote

Following Django Tutorial (CRM)-Part4

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. Rename/Restructure Urls

2. add some style and edit the body on html

<style>
	.lead {
		padding-top: 10px;
		padding-bottom: 10px;
		padding-left: 6px;
		padding-right: 6px;
		margin-top: 10px;
		background-color: #f6f6f6;
		width: 100%;
	}
</style>
<h1>This is all of our leads</h1>
{% for lead in leads %}
	<div class="lead">
		{{ lead.first_name }} {{ lead.last_name }} Age: {{ lead.age }}
	</div>
{% endfor %}

3. Add lead detail view

def lead_detail(request, pk):
	lead = Lead.objects.get(id=pk)
	context = {
		"lead": lead
	}
	return render(request, "lead_detail.html", context)
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>test</title>
</head>
<body>
	<h1>This is the details of {{ lead.first_name }}</h1>
	<p>This person's age: {{ lead.age }}</p>
</body>
</html>

4. add button to go to detail page and go back button

<a href="/leads/{{ lead.pk }}/">{{ lead.first_name }} {{ lead.last_name }}</a>. Age: {{ lead.age }}
<a href="/leads">Go back to leads</a>
<hr />

5. Create views and forms

def lead_create(request):
	return render(request, "lead_create.html")
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>test</title>
</head>
<body>
	<a href="/leads">Go back to leads</a>
	<hr />
	<h1>Create a new lead</h1>
</body>
</html>
urlpatterns = [
	path('', lead_list),
	path('<int:pk>/', lead_detail),
	path('create/', lead_create)
]
urlpatterns = [
	path('', lead_list),
	path('create/', lead_create),
	path('<pk>/', lead_detail)
]
from django import forms


class LeadForm(forms.Form):
	first_name = forms.CharField()
	last_name = forms.CharField()
	age = forms. IntegerField(min_value=0)
form = LeadForm()
if request.method == "POST":
	form = LeadForm(request.POST)
	if form.is_valid():
		first_name = form.cleaned_data['first_name']
		last_name = form.cleaned_data['last_name']
		age = form.cleaned_data['age']
		agent = Agent.objects.first()
		Lead.objects.create(
			first_name=first_name,
			last_name=last_name,
			age=age,
			agent=agent
		)
		return redirect("/leads")
context = {
	"form": form
}
<form method="post">
	{% csrf_token %}
	{{ form.as_p }}
	<button type="submit">Submit</button>
</form>

Done for this post.

Exit mobile version