我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.shortcuts.RequestContext()。
def user_add(request): temp_name = "accounts/accounts-header.html" if request.method == 'POST': form = AddUserForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.set_password(form.cleaned_data['password']) form.save() return HttpResponseRedirect(reverse('user_list'), RequestContext(request)) else: form = AddUserForm() kwargs = { 'form': form, 'request': request, 'temp_name': temp_name, } return render(request, 'accounts/user_add.html', kwargs)
def index(request): uid = request.session.get('user') if uid is None: # main landing page market_items = MarketingItem.objects.all() return render_to_response( 'main/index.html', {'marketing_items': market_items} ) else: # membership page status = StatusReport.objects.all().order_by('-when')[:20] announce_date = date.today() - timedelta(days=30) announce = (Announcement.objects.filter( when__gt=announce_date).order_by('-when') ) usr = User.get_by_id(uid) badges = usr.badges.all() return render_to_response( 'main/user.html', { 'user': usr, 'badges': badges, 'reports': status, 'announce': announce }, context_instance=RequestContext(request), )
def new_arbitrate(request): if request.user.is_anonymous(): return HttpResponseForbidden() if request.user.department is not None: if request.method == 'POST': pdn_h = PdnForm(request.POST, prefix='pdn') cert_h = CertForm(request.POST, prefix='cert') arb_h = ArbitrateForm(request.POST, prefix='arbitrate') if cert_h.is_valid() and arb_h.is_valid() and pdn_h.is_valid(): user = User.objects.create_user(pdn_h.cleaned_data.get('login'), None, pdn_h.cleaned_data.get('password')) user.first_name = pdn_h.cleaned_data.get('first_name') user.last_name = pdn_h.cleaned_data.get('last_name') # ??????? ?????????????? ???? ???????? ???? ???. c = cert_h.save() request.user.department.arbitration_set.create(certificate=c, user=user, activity_info=arb_h.cleaned_data.get('activity_info'), dismissal_date=arb_h.cleaned_data.get('dismissal_date'), office_location=arb_h.cleaned_data.get('office_location'), organization_field=arb_h.cleaned_data.get('organization_field'), name_register=arb_h.cleaned_data.get('name_register'), ) # ? ????????? ?????? ?????? ?????? ??? ???????, ???? ??? ? ????? ???????. # ???? ???-?? ???????? ??????? -- u r welcome user.save() return redirect(arbitrates) else: pdn_h = PdnForm(prefix='pdn') cert_h = CertForm(prefix='cert') arb_h = ArbitrateForm(prefix='arbitrate') return render_to_response("createarbitrate.html", {"pdn": pdn_h, "cert": cert_h, "arbitrate": arb_h, }, context_instance=RequestContext(request)) else: return HttpResponseForbidden()
def new_act(request): if request.user.is_anonymous(): return redirect(login) if is_arbitrate(request.user): return HttpResponseForbidden() if request.method == 'POST': person = PersonForm(request.POST, prefix="person") jud = JudForm(request.POST, prefix='jud') _act = ActForm(request.POST, prefix='act') if person.is_valid() and jud.is_valid() and _act.is_valid(): person = person.save() jud = jud.save() print(jud.name) _act = _act.save(commit=False) _act.person = person _act.jud = jud _act.save() print("success!") return redirect(arbitrates) else: person = PersonForm(prefix='person') jud = JudForm(prefix='jud') ActForm.base_fields['arbitration'] = forms.ModelChoiceField(queryset=request.user.department.arbitration_set) _act = ActForm(prefix='act') return render_to_response("createact.html", {'person': person, 'jud': jud, 'act': _act}, context_instance=RequestContext(request))
def change_act(request, pk): print(pk) if request.user.is_anonymous(): return redirect(login) if len(Act.objects.filter(pk=pk)) == 0: raise Http404 # ?? ????????? ?????, ??? ???? ??????????? ?????? if request.user != Act.objects.get(pk=pk).arbitration.user \ and request.user != Act.objects.get(pk=pk).arbitration.dep: return HttpResponseForbidden() if request.method == 'POST': _act = ChangeActForm(request.POST, prefix='act') if _act.is_valid(): _act.save(commit=False) current_act = Act.objects.get(pk=pk) current_act.start_date = _act.cleaned_data.get('start_date') current_act.end_date = _act.cleaned_data.get('end_date') current_act.is_active = _act.cleaned_data.get('is_active') current_act.info_processing = _act.cleaned_data.get('info_processing',) current_act.creditor_requirements = _act.cleaned_data.get('creditor_requirements') current_act.save() print("redirect doesn't work") return redirect('/act/' + str(pk) + '/') else: current_act = Act.objects.get(pk=pk) _act = ChangeActForm(initial={'start_date': current_act.start_date, 'end_date': current_act.end_date, 'info_processing': current_act.info_processing, 'is_active': current_act.is_active, 'creditor_requirements': current_act.creditor_requirements}, prefix='act') return render_to_response("changeact.html", {'act': _act}, context_instance=RequestContext(request))
def index(request): return render_to_response("index.html", locals(), context_instance=RequestContext(request))
def song(request, key): song = get_object_or_404(Song.objects.all(), key=key) if not os.path.isfile(song.mp3_file) and song.is_composed: writer.write(key) return render_to_response('composer/song.html', { 'song': song, }, RequestContext(request))
def user_del(request, ids): if ids: get_user_model().objects.filter(id=ids).delete() return HttpResponseRedirect(reverse('user_list'), RequestContext(request))
def home(request): return render_to_response("index.html", context_instance=RequestContext(request))