我已经使用Django设置了一个pastebin应用程序,但我希望能够在我的主页模板上创建一个指向每个单独粘贴的链接列表。我一直在尝试一些不同的东西,但我正在努力弄明白。
以下是我目前在模板上的内容,该应用程序名为oxybeles:
<ul>
{% for ????? in ????? %}
<a href="{% url oxybeles_detail item.uuid %}">A Link</a>
{% endfor %}
</ul>
以下是相关的后端代码:
models.py
class PastedItem(models.Model):
uuid = models.CharField(_('identifier'), max_length=36, unique=True)
artist = models.CharField(_('artist'), max_length=50)
album = models.CharField(_('album'), max_length=50)
in_response_to = models.ForeignKey('self', related_name='responses',
blank=True, null=True, verbose_name=_('in response to'))
user = models.ForeignKey(User, related_name="pasted_items",
verbose_name=_('user'))
pasted_at = models.DateTimeField(_('pasted at'), auto_now_add=True)
def __unicode__(self):
return self.uuid
def save(self):
if not self.uuid:
self.uuid = str(uuid4()) # random so it can't be easily guessed
super(PastedItem, self).save()
def get_absolute_url(self):
return ('oxybeles_detail', (), { 'slug': self.uuid })
get_absolute_url = models.permalink(get_absolute_url)
views.py
def new(request, form_class=PastedItemForm, template_name="oxybeles/new.html"):
form = form_class()
if request.method == 'POST':
if request.POST["action"] == "paste":
form = form_class(request.user, request.POST, request.FILES)
if form.is_valid():
item = form.save(commit=False)
item.user = request.user
item.save()
request.user.message_set.create(
message=ugettext("Your album has been saved."))
# some problem with ugettext_lazy here
return HttpResponseRedirect(reverse('oxybeles_detail',
args=(item.uuid,)))
return render_to_response(template_name,
{'form': form,},
context_instance=RequestContext(request))
new = login_required(new)
答案 0 :(得分:1)
for item in items
其中items是一个满是PastedItem的查询集:
items = PastedItem.objects.all()
和项目提供给模板:
{'form':form, 'items': items}