Nothing Special   »   [go: up one dir, main page]

Cs Project Django

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

Overview Of Django

What is Django?
Django is a high-level Python web framework that enables rapid development
of secure and maintainable websites. Built by experienced developers, Django takes
care of much of the hassle of web development, so you can focus on writing your app
without needing to reinvent the wheel. It is free and open source, has a thriving and
active community, great documentation, and many options for free and paid-for
support.

Django helps you write software that is:

Complete
Django follows the "Batteries included" philosophy and provides almost
everything developers might want to do "out of the box". Because everything
you need is part of the one "product", it all works seamlessly together, follows
consistent design principles, and has extensive and up-to-date documentation.
Versatile
Django can be (and has been) used to build almost any type of website — from
content management systems and wikis, through to social networks and news
sites. It can work with any client-side framework, and can deliver content in
almost any format (including HTML, RSS feeds, JSON, XML, etc). The site you
are currently reading is based on Django!

Internally, while it provides choices for almost any functionality you might
want (e.g. several popular databases, templating engines, etc.), it can also be
extended to use other components if needed.

Secure
Django helps developers avoid many common security mistakes by providing
a framework that has been engineered to "do the right things" to protect the
website automatically. For example, Django provides a secure way to manage
user accounts and passwords, avoiding common mistakes like putting session
information in cookies where it is vulnerable (instead cookies just contain a
key, and the actual data is stored in the database) or directly storing
passwords rather than a password hash.

A password hash is a fixed-length value created by sending the password


through a cryptographic hash function. Django can check if an entered password is
correct by running it through the hash function and comparing the output to the
stored hash value. However due to the "one-way" nature of the function, even if a
stored hash value is compromised it is hard for an attacker to work out the original
password.

Django enables protection against many vulnerabilities by default,


including SQL injection, cross-site scripting, cross-site request forgery and
clickjacking (see Website security for more details of such attacks).
Scalable
Django uses a component-based “shared-nothing” architecture (each part of
the architecture is independent of the others, and can hence be replaced or
changed if needed). Having a clear separation between the different parts
means that it can scale for increased traffic by adding hardware at any level:
caching servers, database servers, or application servers. Some of the busiest
sites have successfully scaled Django to meet their demands (e.g. Instagram
and Disqus, to name just two).
Maintainable
Django code is written using design principles and patterns that encourage the
creation of maintainable and reusable code. In particular, it makes use of the
Don't Repeat Yourself (DRY) principle so there is no unnecessary duplication,
reducing the amount of code. Django also promotes the grouping of related
functionality into reusable "applications" and, at a lower level, groups related
code into modules (along the lines of the Model View Controller
(MVC) pattern).
Portable
Django is written in Python, which runs on many platforms. That means that
you are not tied to any particular server platform, and can run your
applications on many flavours of Linux, Windows, and Mac OS X.
Furthermore, Django is well-supported by many web hosting providers, who
often provide specific infrastructure and documentation for hosting Django
sites.
What does Django code look like?
In a traditional data-driven website, a web application waits for HTTP requests from
the web browser (or other client). When a request is received the application works
out what is needed based on the URL and possibly information in POST data
or GET data. Depending on what is required it may then read or write information
from a database or perform other tasks required to satisfy the request. The
application will then return a response to the web browser, often dynamically
creating an HTML page for the browser to display by inserting the retrieved data into
placeholders in an HTML template.
Django web applications typically group the code that handles each of these steps
into separate files:

 URLs: While it is possible to process requests from every single URL via a single function, it
is much more maintainable to write a separate view function to handle each resource. A URL
mapper is used to redirect HTTP requests to the appropriate view based on the request URL.
The URL mapper can also match particular patterns of strings or digits that appear in an
URL, and pass these to a view function as data.
 View: A view is a request handler function, which receives HTTP requests and returns HTTP
responses. Views access the data needed to satisfy requests via models, and delegate the
formatting of the response to templates.
 Models: Models are Python objects that define the structure of an application's data, and
provide mechanisms to manage (add, modify, delete) and query records in the database.
 Templates: A template is a text file defining the structure or layout of a file (such as an
HTML page), with placeholders used to represent actual content. A view can dynamically
create an HTML page using an HTML template, populating it with data from a model. A
template can be used to define the structure of any type of file; it doesn't have to be HTML!
Note: Django refers to this organisation as the "Model View Template (MVT)"
architecture. It has many similarities to the more familiar Model View
Controller architecture.
The sections below will give you an idea of what these main parts of a Django app
look like (we'll go into more detail later on in the course, once we've set up a
development environment).

Sending the request to the right view (urls.py)

A URL mapper is typically stored in a file named urls.py. In the example below, the
mapper (urlpatterns) defines a list of mappings
between routes (specific URL patterns) and corresponding view functions. If an HTTP
Request is received that has a URL matching a specified pattern then the
associated view function will be called and passed the request.

urlpatterns = [

path('admin/', admin.site.urls),
path('book/<int:id>/', views.book_detail, name='book_detail'),
path('catalog/', include('catalog.urls')),
re_path(r'^([0-9]+)/$', views.best),

]
The urlpatterns object is a list of path() and/or re_path() functions (Python lists are
defined using square brackets, where items are separated by commas and may
have an optional trailing comma. For example: [item1, item2, item3,]).
The first argument to both methods is a route (pattern) that will be matched.
The path() method uses angle brackets to define parts of a URL that will be captured
and passed through to the view function as named arguments. The re_path() function
uses a flexible pattern matching approach known as a regular expression. We'll talk
about these in a later article!
The second argument is another function that will be called when the pattern is
matched. The notation views.book_detail indicates that the function is
called book_detail() and can be found in a module called views (i.e. inside a file
named views.py)

Handling the request (views.py)

Views are the heart of the web application, receiving HTTP requests from web clients
and returning HTTP responses. In between, they marshall the other resources of the
framework to access databases, render templates, etc.

The example below shows a minimal view function index(), which could have been
called by our URL mapper in the previous section. Like all view functions it receives
an HttpRequest object as a parameter (request) and returns an HttpResponse object. In this
case we don't do anything with the request, and our response simply returns a hard-
coded string. We'll show you a request that does something more interesting in a
later section.

# filename: views.py (Django view functions)

from django.http import HttpResponse

def index(request):
# Get an HttpRequest - the request parameter
# perform operations using information from the request.
# Return HttpResponse return HttpResponse('Hello from Django!')

Note: A little bit of Python:


 Python modules are "libraries" of functions, stored in separate files, that we might want to
use in our code. Here we import just the HttpResponse object from the django.http module so
that we can use it in our view: from django.http import HttpResponse . There are other ways of
importing some or all objects from a module.
 Functions are declared using the def keyword as shown above, with named parameters listed
in brackets after the name of the function; the whole line ends in a colon. Note how the next
lines are all indented. The indentation is important, as it specifies that the lines of code are
inside that particular block (mandatory indentation is a key feature of Python, and is one
reason that Python code is so easy to read).

Views are usually stored in a file called views.py

Defining data models (models.py)

Django web applications manage and query data through Python objects referred to
as models. Models define the structure of stored data, including the field types and
possibly also their maximum size, default values, selection list options, help text for
documentation, label text for forms, etc. The definition of the model is independent of
the underlying database — you can choose one of several as part of your project
settings. Once you've chosen what database you want to use, you don't need to talk
to it directly at all — you just write your model structure and other code, and Django
handles all the dirty work of communicating with the database for you.
The code snippet below shows a very simple Django model for a Team object.
The Team class is derived from the django class models.Model. It defines the team name
and team level as character fields and specifies a maximum number of characters to
be stored for each record. The team_level can be one of several values, so we define it
as a choice field and provide a mapping between choices to be displayed and data to
be stored, along with a default value.

# filename: models.py

from django.db import models

class Team(models.Model):
team_name = models.CharField(max_length=40)

TEAM_LEVELS = (
('U09', 'Under 09s'),
('U10', 'Under 10s'),
('U11', 'Under 11s'),
... #list other team levels
)
team_level = models.CharField(max_length=3, choices=TEAM_LEVELS, default='U11')
Note: A little bit of Python:
 Python supports "object-oriented programming", a style of programming where we organise
our code into objects, which include related data and functions for operating on that data.
Objects can also inherit/extend/derive from other objects, allowing common behaviour
between related objects to be shared. In Python we use the keyword class to define the
"blueprint" for an object. We can create multiple specific instances of the type of object based
on the model in the class.

So for example, here we have a Team class, which derives from the Model class. This means it
is a model, and will contain all the methods of a model, but we can also give it specialized
features of its own too. In our model we define the fields our database will need to store our
data, giving them specific names. Django uses these definitions, including the field names, to
create the underlying database.

Querying data (views.py)

The Django model provides a simple query API for searching the database. This can
match against a number of fields at a time using different criteria (e.g. exact, case-
insensitive, greater than, etc.), and can support complex statements (for example, you
can specify a search on U11 teams that have a team name that starts with "Fr" or ends
with "al").

The code snippet shows a view function (resource handler) for displaying all of our
U09 teams. The line in bold shows how we can use the model query API to filter for
all records where the team_level field has exactly the text 'U09' (note how this criteria is
passed to the filter() function as an argument with the field name and match type
separated by a double underscore: team_level__exact).

## filename: views.py

from django.shortcuts import render

from .models import Team

def index(request):
list_teams = Team.objects.filter(team_level__exact="U09")
context = {'youngest_teams': list_teams}
return render(request, '/best/index.html', context)

This function uses the render() function to create the HttpResponse that is sent back to
the browser. This function is a shortcut; it creates an HTML file by combining a
specified HTML template and some data to insert in the template (provided in
the variable named "context"). In the next section we show how the template has the
data inserted in it to create the HTML.

Rendering data (HTML templates)

Template systems allow you to specify the structure of an output document, using
placeholders for data that will be filled in when a page is generated. Templates are
often used to create HTML, but can also create other types of document. Django
supports both its native templating system and another popular Python library called
Jinja2 out of the box (it can also be made to support other systems if needed).

The code snippet shows what the HTML template called by the render() function in
the previous section might look like. This template has been written under the
assumption that it will have access to a list variable called youngest_teams when it is
rendered (contained in the context variable inside the render() function above). Inside
the HTML skeleton we have an expression that first checks if
the youngest_teams variable exists, and then iterates it in a for loop. On each iteration
the template displays each team's team_name value in an <li> element.

## filename: best/templates/best/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home page</title>
</head>
<body>
{% if youngest_teams %}
<ul>
{% for team in youngest_teams %}
<li>{{ team.team_name }}</li>
{% endfor %}
</ul>
{% else %}
<p>No teams are available.</p>
{% endif %}
</body>
</html>

What else can you do?


The preceding sections show the main features that you'll use in almost every web
application: URL mapping, views, models and templates. Just a few of the other
things provided by Django include:

 Forms: HTML Forms are used to collect user data for processing on the server. Django
simplifies form creation, validation, and processing.
 User authentication and permissions: Django includes a robust user authentication and
permission system that has been built with security in mind.
 Caching: Creating content dynamically is much more computationally intensive (and slow)
than serving static content. Django provides flexible caching so that you can store all or part
of a rendered page so that it doesn't get re-rendered except when necessary.
 Administration site: The Django administration site is included by default when you create
an app using the basic skeleton. It makes it trivially easy to provide an admin page for site
administrators to create, edit, and view any data models in your site.
 Serialising data: Django makes it easy to serialise and serve your data as XML or JSON. This
can be useful when creating a web service (a website that purely serves data to be consumed
by other applications or sites, and doesn't display anything itself), or when creating a website
in which the client-side code handles all the rendering of data.
SYNOPSIS

INTRODUCTION:

Objective: The objective of the “Social Networking Website” is to provide a platform


which handles the information of the people coming onto the site and maintaining account.
It takes care of all their post information. It also enables them to join groups and post in
different groups. Data will be stored in the database.

PROJECT DESCRIPTION:

The website to be produced is on Social Network. Here there are multiple users. A single
user can sign up to the site by opting a username and password. Then the user can login
using that username and password. Then there are two options available. Either he/she can
join an existing group create a new one. Once he/she became a member of a group the
optionality of posting a post in that group will be made available.

Modules of the software:.

Sign up: User can sign up by providing required information


Login: User enters User Name and password to login this software
application

Groups: Here is the list of all the existing groups that have been made.
Create New Group: Here user can create a new group by providing a
suitable name and description of group.
Post: Here user can post in any of the existing which he/she is a member of.

SOFTWARE REQUIREMENTS:

Language: Python
Framework: Django

Database: My SQL
Source Code

1. Groups: models.py
from django.db import models

from django.utils.text import slugify

import misaka

from django.urls import reverse

from django.contrib.auth import get_user_model

User=get_user_model()

from django import template

register=template.Library()

class Group(models.Model):

name = models.CharField(max_length=256,unique=True)

slug = models.SlugField(allow_unicode=True,unique=True)

description = models.TextField(blank=True,default='')

description_html = models.TextField(editable=False,default='',blank=True)

members = models.ManyToManyField(User,through='GroupMember')
def __str__(self):

return self.name

def save(self,*args,**kwargs):

self.slug = slugify(self.name)

self.description_html = misaka.html(self.description)

super().save(*args,**kwargs)

def get_absolute_url(self):

return reverse('groups:single',kwargs={'slug':self.slug})

class Meta:

ordering=['name']

class GroupMember(models.Model):

group =
models.ForeignKey(Group,related_name='memberships',on_delete=models.CASCADE
)

user =
models.ForeignKey(User,on_delete=models.CASCADE,related_name='user_groups')

def __str__(self):

return self.user.username

class Meta:

unique_together = ('group','user')

2. Groups: views.py
from django.contrib import messages

from django.contrib.auth.mixins import(

LoginRequiredMixin,

PermissionRequiredMixin

from django.urls import reverse

from django.db import IntegrityError

from django.shortcuts import get_object_or_404

from django.views import generic

from groups.models import Group,GroupMember

from . import models

class CreateGroup(LoginRequiredMixin, generic.CreateView):

fields = ("name", "description")

model = Group

class SingleGroup(generic.DetailView):

model = Group

class ListGroups(generic.ListView):

context_object_name='groups'

model = Group
class JoinGroup(LoginRequiredMixin, generic.RedirectView):

def get_redirect_url(self, *args, **kwargs):

return reverse("groups:single",kwargs={"slug": self.kwargs.get("slug")})

def get(self, request, *args, **kwargs):

group = get_object_or_404(Group,slug=self.kwargs.get("slug"))

try:

GroupMember.objects.create(user=self.request.user,group=group)

except IntegrityError:

messages.warning(self.request,("Warning, already a member of


{}".format(group.name)))

else:

messages.success(self.request,"You are now a member of the {}


group.".format(group.name))

return super().get(request, *args, **kwargs)

class LeaveGroup(LoginRequiredMixin, generic.RedirectView):

def get_redirect_url(self, *args, **kwargs):

return reverse("groups:single",kwargs={"slug": self.kwargs.get("slug")})

def get(self, request, *args, **kwargs):

try:

membership = models.GroupMember.objects.filter(

user=self.request.user,

group__slug=self.kwargs.get("slug")

).get()

except models.GroupMember.DoesNotExist:
messages.warning(

self.request,

"You can't leave this group because you aren't in it."

else:

membership.delete()

messages.success(

self.request,

"You have successfully left this group."

return super().get(request, *args, **kwargs)


3. Groups: urls.py

from django.urls import re_path

from . import views

app_name = 'groups'

urlpatterns = [
re_path(r"^$", views.ListGroups.as_view(), name="all"),
re_path(r"^new/$", views.CreateGroup.as_view(), name="create"),
re_path(r"^posts/in/(?P<slug>[-
\w]+)/$",views.SingleGroup.as_view(),name="single"),
re_path(r"^join/(?P<slug>[-\w]+)/$",views.JoinGroup.as_view(),name="join"),
re_path(r"^leave/(?P<slug>[-\w]+)/$",views.LeaveGroup.as_view(),name="leave"),
]
4. Posts: models.py

from django.conf import settings


from django.urls import reverse
from django.db import models
import misaka
from groups.models import Group
from django.contrib.auth import get_user_model
User = get_user_model()
class Post(models.Model):
user = models.ForeignKey(User,
related_name="posts",on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
group = models.ForeignKey(Group, related_name="posts",null=True,
blank=True,on_delete=models.CASCADE)

def __str__(self):
return self.message

def save(self, *args, **kwargs):


self.message_html = misaka.html(self.message)
super().save(*args, **kwargs)

def get_absolute_url(self):
return reverse(
"posts:single",
kwargs={
"username": self.user.username,
"pk": self.pk
}
)
class Meta:
ordering = ["-created_at"]
unique_together = ["user", "message"]
5. Posts: views.py

from django.contrib import messages


from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.http import Http404
from django.views import generic
from braces.views import SelectRelatedMixin
from . import forms
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()

class PostList(SelectRelatedMixin, generic.ListView):


model = models.Post
select_related = ("user", "group")

class UserPosts(generic.ListView):
model = models.Post
template_name = "posts/user_post_list.html"

def get_queryset(self):
try:
self.post_user = User.objects.prefetch_related("posts").get(
username__iexact=self.kwargs.get("username")
)
except User.DoesNotExist:
raise Http404
else:
return self.post_user.posts.all()

def get_context_data(self, **kwargs):


context = super().get_context_data(**kwargs)
context["post_user"] = self.post_user
return context

class PostDetail(SelectRelatedMixin, generic.DetailView):


model = models.Post
select_related = ("user", "group")

def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(
user__username__iexact=self.kwargs.get("username")
)
class CreatePost(LoginRequiredMixin, SelectRelatedMixin, generic.CreateView):
# form_class = forms.PostForm
fields = ('message','group')
model = models.Post

# def get_form_kwargs(self):
# kwargs = super().get_form_kwargs()
# kwargs.update({"user": self.request.user})
# return kwargs

def form_valid(self, form):


self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)
class DeletePost(LoginRequiredMixin, SelectRelatedMixin, generic.DeleteView):
model = models.Post
select_related = ("user", "group")
success_url = reverse_lazy("posts:all")

def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user_id=self.request.user.id)

def delete(self, *args, **kwargs):


messages.success(self.request, "Post Deleted")
return super().delete(*args,**kwargs)
6. Posts: urls.py

from django.urls import re_path


from . import views
app_name = 'posts'
urlpatterns=[
re_path(r'^$',views.PostList.as_view(),name='all'),
re_path(r'new/$',views.CreatePost.as_view(),name='create'),
re_path(r'by/(?P<username>[-
\w]+)/',views.UserPosts.as_view(),name='for_user'),
re_path(r'by/(?P<username>[-
\w]+)/(?P<pk>\d+)/$',views.PostDetail.as_view(),name='single'),
re_path(r'delete/(?P<pk>\d+)/$',views.DeletePost.as_view(),name='delete'),
]
Templates
7. base.html

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Star Social</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"
rel="stylesheet" id="bootstrap-css">
<script
src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/jquery-3.4.1.js" integrity="sha256-
WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat"
rel="stylesheet">
<script src="//cdn.jsdelivr.net/medium-editor/latest/js/medium-
editor.min.js"></script>
<link rel="stylesheet" href="{% static 'simplesocial/css/master.css'%}">
<script type="text/javascript" src="{% static 'simplesocial/js/master.js' %}">
</script>
</head>
<body>
<!--
<nav class="navbar mynav" role="navigation" id='navbar'>
<div class="container">
<a class="navbar-brand mynav" href="{% url 'home' %}">Star Social</a>
<ul class="nav navbar-nav">
{% if user.is_authenticated %}
<li><a href="{% url 'posts:create' %}" class="btn btn-simple">Post</a></li>
<li><a href="{% url 'groups:all' %}" class="btn btn-
simple">Groups</a></li>
<li><a href="{% url 'groups:create' %}" class="btn btn-simple">Create
Group</a></li>
<li><a href="{% url 'accounts:logout' %}" class="btn btn-simple">Log
out</a></li>
{% else %}
<li><a href="{% url 'groups:all' %}"class="btn btn-simple">Groups</a></li>
<li><a href="{% url 'accounts:login' %}" class="btn btn-simple">Log
in</a></li>
<li><a href="{% url 'accounts:signup' %}" class="btn btn-simple">Sign
up</a></li>
{% endif %}
</ul>
</div>
</nav>-->
<nav class="navbar navbar-default">

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-collapse-2">
<ul class="nav navbar-nav navbar-right">

{% if user.is_authenticated %}
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'posts:create' %}">Post</a></li>
<li><a href="{% url 'groups:all' %}">Groups</a></li>
<li><a href="{% url 'groups:create' %}">Create Group</a></li>
<li><a href="{% url 'accounts:logout' %}">Log out</a></li>
{% else %}
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'groups:all' %}">Groups</a></li>
<li><a href="{% url 'accounts:login' %}">Log in</a></li>
<li><a href="{% url 'accounts:signup' %}">Sign up</a></li>
{% endif %}
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container -->
</nav><!-- /.navbar -->
<div class="container mycontent">
{% block content %}
{% endblock %}
</div>
</body>

</html>
8. Index.html

{% extends "base.html" %}
{% block content %}
<div class="section">
<h1>Welcome To Paradise!</h1>
<div class="video-container">
<div class="color-overlay"></div>
<video autoplay loop muted>
<source src="{{ media }}video.mp4" type="video/mp4">
</video>
</div>
{% endblock %}

9. Test.html
{% extends "base.html" %}
{% block content %}
<h1>You are now logged in!</h1>
{% endblock %}

10. Thanks.html
{% extends "base.html" %}
{% block content %}
<h1>Thanks for visiting, come back soon!</h1>
{% endblock %}

11. Login.html
{% extends "base.html" %}
{% load bootstrap3 %}
{% block content %}
<div class="container">
<h1>Log In</h1>
<form method="post">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-default" value="Login">
</form>
</div>
{% endblock %}
12. Signup.html
{% extends "base.html" %}
{% load bootstrap3 %}
{% block content %}
<div class="container">
<h1>Sign Up</h1>
<form method="post">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-default" value="Sign Up">
</form>
</div>
{% endblock %}

13. Group_base.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
{% block pregroup %}
{% endblock %}
{% block group_content %}
{% endblock %}
{% block postgroup %}
{% endblock %}
</div>
</div>
{% endblock %}
14. Group_detail.html

{% extends "groups/group_base.html" %}
{% block pregroup %}
<h1>{{group.name}}</h1>
<h2> Member Count: {{group.members.count}}</h2>
<div class="content">

{% if user in group.members.all %}
<a href="{% url 'groups:leave' slug=group.slug
%}" class="btn btn-lg btn-fill btn-warning"><span class="glyphicon glyphicon-
remove-circle"></span> Leave</a>
{% else %}
<a href="{% url 'groups:join' slug=group.slug
%}" class="btn btn-lg btn-fill btn-warning"><span class="glyphicon glyphicon-ok-
circle"></span> Join</a>
{% endif %}

</div>

{% endblock %}
{% block group_content %}

<div class="col-md-8">
{% if group.posts.count == 0 %}
<h2>No posts in this group yet!</h2>

{% else %}

{% for post in group.posts.all %}


{% include "posts/_post.html" %}
{% endfor %}

{% endif %}
</div>
{% endblock %}
15. Group_form.html

{% extends "groups/group_base.html" %}
{% load bootstrap3 %}
{% block group_content %}
<h4>Create A New Group</h4>
<form method="POST" action="{% url 'groups:create' %}" id="groupForm">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Create" class="btn btn-primary btn-large">
</form>
{% endblock %}

16. Group_list.html

{% extends "groups/group_base.html" %}
{% block pregroup %}
<div class="col-md-4">
<div class="content">
{% if user.is_authenticated %}
<h2>Welocome Back!
<a href="{% url 'posts:for_user' username=user.username%}">
@{{user.username}}
</a>
</h2>
{% endif %}
<h2>Groups</h2>
<p>Welcome to the Groups page!</p>
</div>
{% if user.is_authenticated %}
<a href="{% url 'groups:create' %}" class="btn btn-md btn-fill btn-warning">
<span class='glyphicon glyphicon-plus-sign'>Create New Group</span>
</a>
{% endif %}
</div>
{% endblock %}

{% block group_content %}
<div class="col-md-8">
<div class="list-group">
{% for group in object_list %}
<a class="list-group-item" href="{% url 'groups:single' slug=group.slug %}">
<h3 class="title list-group-item-heading">{{group.name}}</h3>
<div class="list-group-item-text container-fluid">
{{group.description_html|safe}}
<div class="row">
<div class="col-md-4">
<span class="badge">{{ group.members.count }}</span> member{{
group.members.count|pluralize }}
</div>
<div class="col-md-4">
<span class="badge">{{ group.posts.count }}</span> post{{
group.posts.count|pluralize }}
</div>
</div>
</div>
</a>

{% endfor %}
</div>
</div>
{% endblock %}

17. _post.html

<div class="post media">


<h3>{{ post.message_html|safe }}</h3>
<div class="media-body">
<strong>{{ post.user.username }}</strong>
<h5 class="media-heading">
<span class="username"><a href="{% url 'posts:for_user'
username=post.user.username %}">@{{ post.user.username }}</a></span>
<time class="time"><a href="{% url 'posts:single'
username=post.user.username pk=post.pk %}">{{ post.created_at }}</a></time>
{% if post.group %}
<span class="group-name">in <a href="{% url 'groups:single'
slug=post.group.slug %}">{{ post.group.name }}</a></span>
{% endif %}
</h5>

<div class="media-footer">
{% if user.is_authenticated and post.user == user and not
hide_delete %}
<a href="{% url 'posts:delete' pk=post.pk %}"
title="delete" class="btn btn-simple">
<span class="glyphicon glyphicon-remove
text-danger" aria-hidden="true"></span>
<span class="text-danger icon-
label">Delete</span>
</a>
{% endif %}

</div>
</div>
</div>

18. Post_base.html
{% extends "base.html" %}

{% block content %}
<div class="posts-page">
<div class="container">
<div class="row">
{% block prepost%}{% endblock %}
{% block post_content %}{% endblock %}
{% block post_post%}{% endblock %}
</div>
</div>
</div>
{% endblock %}
19. Post_list.html

{% extends "posts/post_base.html" %}
{% block pre_post_content %}
<div class="col-md-4">
{% if request.user.is_authenticated %}
<div class="card card-with-shadow">
<div class="content">
<h5 class="title">Your Groups</h5>
<ul class="list-unstyled">
{# {% get_user_groups as user_groups %}#}
{% for member_group in get_user_groups %}
<li class="group li-with-bullet">
<a href="{% url 'groups:single'
slug=member_group.group.slug %}">{{ member_group.group.name }}</a>
</li>
{% endfor %}

</ul>
</div>
</div>
{% endif %}
<div class="card card-with-shadow">
<div class="content">
<h5 class="title">All groups</h5>
<ul class="list-unstyled">
{# {% get_other_groups as other_groups %}#}
{% for other_group in get_other_groups %}
<li class="group li-with-bullet"><a href="{% url
'groups:single' slug=other_group.slug %}">{{ other_group.name }}</a></li>
{% endfor %}
</ul>

</div>
</div>
</div>
{% endblock %}
{% block post_content %}
<div class="col-md-8">
{% for post in post_list %}
{% include "posts/_post.html" %}
{% endfor %}
</div>
{% endblock %}

20. User_post_list.html

{% extends "posts/post_base.html" %}

{% block prepost %}
<div class="col-md-4">
<h1>@{{ post_user.username }}</h1>
<p>Post History</p>
</div>
{% endblock %}

{% block post_content %}
<div class="col-md-8">
{% for post in post_list %}
{% include "posts/_post.html" %}
{% endfor %}
</div>
{% endblock %}
Output:

You might also like