Question: 1. Please use Python 3 and django (1,8) to create the following program. Please show all outputs. Here are the import and Post model for

1. Please use Python 3 and django (1,8) to create the following program. Please show all outputs.

Here are the import and Post model for myapp/models.py

from django.db import models from django.contrib.auth.models import User class Post(models.Model): title = models.CharField(max_length=128) text = models.TextField(blank=True) author = models.ForeignKey(User) created_date = models.DateTimeField(auto_now_add=True) modified_date = models.DateTimeField(auto_now=True) published_date = models.DateTimeField(blank=True, null=True) 

Here is the PostTestCase that we write in myapp/tests.py:

from django.test import TestCase from django.contrib.auth.models import User from myapp.models import Post class PostTestCase(TestCase): fixtures = ['myapp/myblog_test_fixture.json',] def setUp(self): self.user = User.objects.get(pk=1) def test_string_representation(self): expected = "This is a title" p1 = Post(title=expected) actual = str(p1) self.assertEqual(expected, actual) 

And here's the `__str__` method for a Post that we use to pass our test:

 def __str__(self): return self.title 

Here is what your myapp/admin.py should contain following this video:

from django.contrib import admin from myapp.models import Post admin.site.register(Post) 

Here's our category model, which goes in myapp/models.py

class Category(models.Model): name = models.CharField(max_length=128) description = models.TextField(blank=True) posts = models.ManyToManyField(Post, blank=True, related_name='categories') 

We register our new model in the myapp/admin.py

... from myapp.models import Post, Category # Add this Category import ... admin.site.register(Category) 

Here is our category test class, for myapp/tests.py

class CategoryTestCase(TestCase): def test_string_representation(self): expected = "A category" c1 = Category(name=expected) actual = str(c1) self.assertEqual(expected, actual) 

And the __str__ method for Category that we provide to make it pass the test

def __str__(self): return self.name 

Here is our FrontEndTestCase for myapp/tests.py:

# You'll need these additional imports at the top of myapp/tests.py import datetime from django.utils.timezone import utc class FrontEndTestCase(TestCase): fixtures = ['myapp/myblog_test_fixture.json',] def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) for count in range(1, 11): post = Post(title="Test Post %d Title" % count, text="foo", author=author) if count < 6: pubdate = self.now - self.timedelta * count post.published_date = pubdate post.save() def test_list_only_published(self): resp = self.client.get('/') for count in range(1, 11): title = "Test Post %d Title" % count if count < 6: self.assertContains(resp, title, count=1) else: self.assertNotContains(resp, title) 

Here is the content of the "stub" list_view in myapp/views.py:

from django.shortcuts import render from django.template import RequestContext, loader from django.http import HttpResponse, HttpResponseRedirect, Http404 from myapp.models import Post def list_view(request): return HttpResponse("HERE!", "text/html") 

Here is the myapp/urls.py once you've added the list_view to the urls patterns:

from django.conf.urls import url from myapp.views import list_view urlpatterns = [ url(r'^$', list_view, name="blog_index"), ] 

And here is myblog/urls.py once we've included myapp.urls in the urlpatterns:

from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ # Examples: # url(r'^$', 'myblog.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^', include('myapp.urls')), # Add this line! url(r'^admin/', include(admin.site.urls)), ] 

Here is myblog/templates/base.html:

   My Django Blog   
{% block content %} {% endblock content %}

Here is myblog/templates/list.html:

{% extends "base.html" %}{% block content %} 

Recent Posts

{% for post in posts %}

{{ post }}

Posted by {{ post.author.name }} &mdash {{ post.published_date }}

    {% for category in post.categories.all %}
  • {{ category }}
  • {% endfor %}
{% endfor %} {% endblock %}

We edit our myblog/settings.py TEMPLATES['DIRS'] as follows:

... 'DIRS': [os.path.join(BASE_DIR, 'myblog/templates'),], ... 

Finally, we return to our list_view and complete it as follows:

def list_view(request): published = Post.objects.exclude(published_date__exact=None) posts = published.order_by('-published_date') template = loader.get_template('list.html') context = RequestContext(request, {'posts': posts}) body = template.render(context) return HttpResponse(body, content_type="text/html") 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!