python - 我不明白 Django 中的测试,你能帮帮我吗?

标签 python django

我在 Django 和 Python 中进行测试时遇到了困难,对于我的最终项目,我正在制作一个论坛网站,但我真的不知道我的测试应该如何进行或测试应该是什么。这是来自 mysite 文件的 View 页面。如果用户已登录,有人可以指导我完成我应该测试的内容吗?

from django.core.urlresolvers import reverse
from settings import MEDIA_ROOT, MEDIA_URL
from django.shortcuts import redirect, render_to_response
from django.template import loader, Context, RequestContext
from mysite2.forum.models import *

def list_forums(request):
     """Main listing."""
     forums = Forum.objects.all()
     return render_to_response("forum/list_forums.html", {"forums":forums},      context_instance=RequestContext(request))



def mk_paginator(request, items, num_items):
    """Create and return a paginator."""
    paginator = Paginator(items, num_items)
    try: page = int(request.GET.get("page", '1'))
    except ValueError: page = 1

    try:
        items = paginator.page(page)
    except (InvalidPage, EmptyPage):
        items = paginator.page(paginator.num_pages)
    return items


def list_threads(request, forum_slug):
    """Listing of threads in a forum."""
    threads = Thread.objects.filter(forum__slug=forum_slug).order_by("-created")
    threads = mk_paginator(request, threads, 20)
     template_data = {'threads': threads} 
    return render_to_response("forum/list_threads.html", template_data, context_instance=RequestContext(request))

def list_posts(request, forum_slug, thread_slug):
    """Listing of posts in a thread."""
    posts = Post.objects.filter(thread__slug=thread_slug,  thread__forum__slug=forum_slug).order_by("created")
    posts = mk_paginator(request, posts, 15)
    thread = Thread.objects.get(slug=thread_slug)
    template_data = {'posts': posts, 'thread' : thread}
    return render_to_response("forum/list_posts.html", template_data, context_instance=RequestContext(request))

def post(request, ptype, pk):
    """Display a post form."""
    action = reverse("mysite2.forum.views.%s" % ptype, args=[pk])
    if ptype == "new_thread":
        title = "Start New Topic"
        subject = ''
    elif ptype == "reply":
        title = "Reply"
        subject = "Re: " + Thread.objects.get(pk=pk).title
    template_data = {'action': action, 'title' : title, 'subject' : subject}

    return render_to_response("forum/post.html", template_data, context_instance=RequestContext(request))

def new_thread(request, pk): 
    """Start a new thread."""
    p = request.POST
    if p["subject"] and p["body"]:
        forum = Forum.objects.get(pk=pk)
        thread = Thread.objects.create(forum=forum, title=p["subject"], creator=request.user)
        Post.objects.create(thread=thread, title=p["subject"], body=p["body"], creator=request.user)
     return HttpResponseRedirect(reverse("dbe.forum.views.forum", args=[pk]))

def reply(request, pk):
    """Reply to a thread."""
     p = request.POST
    if p["body"]:
        thread = Thread.objects.get(pk=pk)
        post = Post.objects.create(thread=thread, title=p["subject"],         body=p["body"],
        creator=request.user)
     return HttpResponseRedirect(reverse("dbe.forum.views.thread", args=[pk]) +      "?page=last")

最佳答案

首先阅读Django testing documentation .您可能还想阅读 this book .它在某些方面已经过时,但现在的测试仍然与 1.1 中的几乎相同。

在 SO 回答中涉及的主题有点多。

关于python - 我不明白 Django 中的测试,你能帮帮我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408231/

相关文章:

python - sqlalchemy `sum`、 `average`、 `min`、 `max` 的简单示例

python - 使用 asyncpg 插入多行的最佳方法

python - 如何在 Django 中获取原始请求 header ?

django - 表单字段取决于另一个字段值

python - 使用 python 请求在 HTTP POST 上规避错误 414

python - 如何在 matplotlib python 中设置确切的日期刻度

django - 用于 Django 开发的 Python 2.7 与 Python 3.3

python - Django 存储 AWS_QUERYSTRING_AUTH 不工作

python - 序列化单个对象?

python - 有没有办法在 python 中对具有不匹配索引的数组进行子集化?