I have set up a pastebin app with Django but I would like to be able to create a list of links to each individual paste on my homepage's template. I have been trying out a number of different things but am struggling to figure it out.
Here is what I have so far on the template, the app is named oxybeles:
<ul>
{% for ????? in ????? %}
<a href="{% url oxybeles_detail item.uuid %}">A Link</a>
{% endfor %}
</ul>
And here is the relevant back-end code:
models.py
class PastedItem(models.Model):
uuid = models.CharField(_('identifier'), max_length=36, uniqu开发者_如何学运维e=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)
for item in items
where items is a queryset full of PastedItem's:
items = PastedItem.objects.all()
and items is proffered to the template:
{'form':form, 'items': items}
精彩评论