我正在构建一个django'留言簿'应用程序,让访问者留下评论。但我一直得到'django导入错误:没有名为comment的模块'。我该怎么解决? 当我单击提交按钮时会弹出错误回溯,但奇怪的是用户输入在DB中保存得很好。
回溯:
ImportError at /comment/1/like/
No module named 'comment'
Request Method: GET
Request URL: http://192.168.56.101:8000/comment/1/like/?csrfmiddlewaretoken=2fPYLRu9acVTcRDxfgXqwYEFMxNs2dbb&comment_id=1
Django Version: 1.7.6
Exception Type: ImportError
Exception Value:
No module named 'comment'
Exception Location: /home/web/venv/lib/python3.4/importlib/__init__.py in import_module, line 109
Python Executable: /home/web/venv/bin/python
Python Version: 3.4.2
Views.py:
from django.shortcuts import render, redirect, get_object_or_404
from guestbook.models import Comment
def index(request):
context = {
"comments" : Comment.objects.all()
}
return render(request, "index.html", context)
def comment_submit(request):
try:
name = request.POST["name"]
contents = request.POST["contents"]
except (KeyError):
return redirect('index')
else:
comment = Comment(name = name, contents = contents)
comment.save()
return redirect('index')
def comment_like(request, comment_id):
comment = get_object_or_404(Comment, id=comment_id)
comment.like = comment.like + 1
comment.save()
return redirect('index')
Models.py:
from django.db import models
class Comment(models.Model):
name = models.CharField("User Name", max_length=100)
contents = models.CharField("Contents", max_length=100)
like = models.IntegerField("Like", default=0)
def __str__(self):
return "%s: %s" % (self.name, self.contents)
URLS.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'guestbook.views.index', name="index"),
url(r'^comment/submit/$', 'guestbook.views.comment_submit', name='comment_submit'),
url(r'^comment/like/$', 'guestbook.views.comment_like', name='comment_like'),
url(r'^comment/(?P<comment_id>\d+)/like/$', 'comment.views.comment_like'),
)
模板:
{% if error_message %}
<script>
alert('{{error_message}}')
</script>
{% endif %}
<br />
<div class="container">
<div class="jumbotron">
<h1>Welcome to Guestbook!</h1>
<br />
<form method="POST" action="/comment/submit/" class="form-horizontal">
{% csrf_token %}
<div class="form-group ">
<label for="name" class="col-sm-2 control-label">이름</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="name" name="name" placeholder="이름">
</div>
</div>
<div class="form-group ">
<label for="contents" class="col-sm-2 control-label">내용</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="contents" name="contents" placeholder="내용">
</div>
</div>
<div class="form-group ">
<div class="col-sm-10">
<input type="submit" class="register btn btn-default" id="register" value="등록">
</div>
</div>
</form>
<div>
<table class="list table">
<!-- Get data from model, which is saved by using Admin -->
{% for comment in comments %}
<tr>
<td>{{comment.name}}: {{comment.contents}} +{{comment.like}}</td>
<td>
<form method="GET" action="comment/{{comment.id}}/like/">
{% csrf_token %}
<input type="hidden" value="{{comment.id}}" name="comment_id">
<input type="submit" value="추천">
</form>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>