本文整理匯總了Python中django.shortcuts.get_object_or_404方法的典型用法代碼示例。如果您正苦於以下問題:Python shortcuts.get_object_or_404方法的具體用法?Python shortcuts.get_object_or_404怎麽用?Python shortcuts.get_object_or_404使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.shortcuts
的用法示例。
在下文中一共展示了shortcuts.get_object_or_404方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: contact_detail
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def contact_detail(request, pk):
if request.method == 'POST':
data = (request.POST.get(key) for key in ('name', 'fone', 'email'))
contact = Contact.objects.get(pk=pk)
contact.name, contact.fone, contact.email = data
contact.save()
else:
contact = get_object_or_404(Contact, pk=pk)
response = dict(
name=contact.name,
avatar=contact.avatar(),
email=contact.email,
phone=contact.fone,
url=resolve_url('contact-details', pk=contact.pk)
)
return JsonResponse(response)
示例2: delete_product
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def delete_product(request, pk, group):
from django.db.models import ProtectedError
product = get_object_or_404(Product, pk=pk)
if request.method == 'POST':
try:
product.delete()
Inventory.objects.filter(product=product).delete()
messages.success(request, _("Product deleted"))
except ProtectedError:
messages.error(request, _('Cannot delete product'))
return redirect(list_products, group)
action = request.path
return render(request, 'products/remove.html', locals())
示例3: download_products
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def download_products(request, group="all"):
"""
Downloads entire product DB or just ones belonging to a group
"""
filename = "products"
data = u"ID\tCODE\tTITLE\tPURCHASE_PRICE\tSALES_PRICE\tSTOCKED\n"
if group == "all":
products = Product.objects.all()
else:
category = get_object_or_404(ProductCategory, slug=group)
products = category.get_products()
filename = group
# @FIXME: Add total stocked amount to product
# currently the last column is a placeholder for stock counts in inventory uploads
for p in products:
row = [unicode(i) for i in (p.pk, p.code, p.title,
p.price_purchase_stock,
p.price_sales_stock, 0)]
data += "\t".join(row) + "\n"
return send_csv(data, filename)
示例4: view
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def view(request, pk, group='all'):
c = get_object_or_404(Customer, pk=pk)
data = prepare_view(request, group)
data['title'] = c.name
data['orders'] = Order.objects.filter(
customer__lft__gte=c.lft,
customer__rght__lte=c.rght,
customer__tree_id=c.tree_id
)
if c.email:
data['notes'] = Note.objects.filter(recipient=c.email)
data['customer'] = c
request.session['return_to'] = request.path
return render(request, 'customers/view.html', data)
示例5: move
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def move(request, pk, new_parent=None):
"""
Moves a customer under another customer
"""
customer = get_object_or_404(Customer, pk=pk)
if new_parent is not None:
if int(new_parent) == 0:
new_parent = None
msg = _(u"Customer %s moved to top level") % customer
else:
new_parent = Customer.objects.get(pk=new_parent)
d = {'customer': customer, 'target': new_parent}
msg = _(u"Customer %(customer)s moved to %(target)s") % d
try:
customer.move_to(new_parent)
customer.save() # To update fullname
messages.success(request, msg)
except Exception as e:
messages.error(request, e)
return redirect(customer)
return render(request, "customers/move.html", locals())
示例6: edit_calendar
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def edit_calendar(request, pk=None, view="week"):
from servo.models.calendar import CalendarForm
calendar = Calendar(user=request.user)
if pk:
calendar = get_object_or_404(Calendar, pk=pk)
if not calendar.user == request.user:
messages.error(request, _('You can only edit your own calendar'))
return redirect(calendars)
if request.method == "POST":
form = CalendarForm(request.POST, instance=calendar)
if form.is_valid():
calendar = form.save()
messages.success(request, _("Calendar saved"))
return redirect(view_calendar, calendar.pk, 'week')
form = CalendarForm(instance=calendar)
data = {'title': calendar.title}
data['form'] = form
data['action'] = request.path
return render(request, "accounts/calendar_form.html", data)
示例7: delete_device
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def delete_device(request, product_line, model, pk):
dev = get_object_or_404(Device, pk=pk)
if request.method == 'POST':
from django.db.models import ProtectedError
try:
dev.delete()
messages.success(request, _("Device deleted"))
except ProtectedError:
messages.error(request, _("Cannot delete device with GSX repairs"))
return redirect(dev)
return redirect(index)
data = {'action': request.path}
data['device'] = dev
return render(request, "devices/remove.html", data)
示例8: update_gsx_details
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def update_gsx_details(request, pk):
"""
Updates devices GSX warranty details
"""
device = get_object_or_404(Device, pk=pk)
try:
GsxAccount.default(request.user)
device.update_gsx_details()
messages.success(request, _("Warranty status updated successfully"))
except Exception as e:
messages.error(request, e)
if request.session.get('return_to'):
return redirect(request.session['return_to'])
return redirect(device)
示例9: remove_user
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def remove_user(request, pk, user_id):
"""
Removes this user from the follower list, unsets assignee
"""
order = get_object_or_404(Order, pk=pk)
user = get_object_or_404(User, pk=user_id)
try:
order.remove_follower(user)
if user == order.user:
order.set_user(None, request.user)
msg = _('User %s removed from followers') % user
order.notify("unset_user", msg, request.user)
except Exception as e:
messages.error(request, e)
return redirect(order)
示例10: remove_product
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def remove_product(request, pk, item_id):
order = get_object_or_404(Order, pk=pk)
# The following is to help those who hit Back after removing a product
try:
item = ServiceOrderItem.objects.get(pk=item_id)
except ServiceOrderItem.DoesNotExist:
messages.error(request, _("Order item does not exist"))
return redirect(order)
if request.method == 'POST':
msg = order.remove_product(item, request.user)
messages.info(request, msg)
return redirect(order)
return render(request, 'orders/remove_product.html', locals())
示例11: create_po
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def create_po(request, product_id=None, order_id=None):
"""
Creates a new Purchase Order
"""
po = PurchaseOrder(created_by=request.user)
po.location = request.user.get_location()
po.save()
if order_id is not None:
po.sales_order = get_object_or_404(Order, pk=order_id)
po.save()
for i in ServiceOrderItem.objects.filter(order_id=order_id):
po.add_product(i, amount=1, user=request.user)
if product_id is not None:
product = get_object_or_404(Product, pk=product_id)
po.add_product(product, amount=1, user=request.user)
messages.success(request, _("Purchase Order %d created" % po.pk))
return redirect(edit_po, po.pk)
示例12: copy
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def copy(request, pk):
"""Copy a note with its attachments and labels."""
note = get_object_or_404(Note, pk=pk)
new_note = Note(created_by=request.user)
new_note.body = note.body
new_note.order = note.order
new_note.subject = note.subject
new_note.save()
new_note.labels = note.labels.all()
for a in note.attachments.all(): # also copy the attachments
a.pk = None
a.content_object = new_note
a.save()
new_note.attachments.add(a)
return redirect(edit, pk=new_note.pk, order_id=note.order_id)
示例13: delete_note
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def delete_note(request, pk):
"""
Deletes a note
"""
note = get_object_or_404(Note, pk=pk)
if request.method == 'POST':
note.delete()
messages.success(request, _("Note deleted"))
if request.session.get('return_to'):
url = request.session.get('return_to')
del(request.session['return_to'])
elif note.order_id:
url = note.order.get_absolute_url()
return redirect(url)
return render(request, 'notes/remove.html', {'note': note})
示例14: toggle_flag
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def toggle_flag(request, kind, pk, flag):
"""
Toggles a flag of a note (read/unread, flagged/not, reported/not)
"""
if kind == 'articles':
note = get_object_or_404(Article, pk=pk)
if flag == 'flagged':
note.toggle_flagged(request.user)
return HttpResponse(note.get_flagged_title(request.user))
if flag == 'read':
note.toggle_read(request.user)
return HttpResponse(note.get_read_title(request.user))
field = 'is_%s' % flag
note = get_object_or_404(Note, pk=pk)
attr = getattr(note, field)
setattr(note, field, not attr)
note.save()
return HttpResponse(getattr(note, 'get_%s_title' % flag)())
示例15: view_note
# 需要導入模塊: from django import shortcuts [as 別名]
# 或者: from django.shortcuts import get_object_or_404 [as 別名]
def view_note(request, kind, pk):
data = prep_list_view(request, kind)
if kind == 'articles':
note = get_object_or_404(Article, pk=pk)
data['read_title'] = note.get_read_title(request.user)
data['flagged_title'] = note.get_flagged_title(request.user)
else:
note = get_object_or_404(Note, pk=pk)
data['title'] = note.get_title()
data['note'] = note
if kind == 'escalations':
return render(request, "notes/view_escalation.html", data)
if kind == 'articles':
return render(request, "notes/view_article.html", data)
return render(request, "notes/view_note.html", data)