將寫代碼過(guò)程中較好的代碼做個(gè)記錄,下面代碼段是關(guān)于Django CheatSheet的代碼,希望能對(duì)大家也有幫助。
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),濟(jì)南網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:濟(jì)南等地區(qū)。濟(jì)南做網(wǎng)站價(jià)格咨詢:028-86922220from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.models import User
from django.http import HttpRequest
from django.conf import settings
request.GET.keys()
return render_to_response('content/auktionen/erstellen/wizard/zusammenfassung.html', locals())
return render('content/auktionen/erstellen/cc_wizard/zusammenfassung.html', locals(), request)
return HttpResponseRedirect('/login/?next=%s' % request.path)
request.GET.get('q', '')
username = request.POST['username']
request.method == 'POST'
HttpRequest.is_ajax()
p = get_object_or_404(Poll, pk=poll_id)
get_list_or_404()
return render_to_response('polls/detail.html', {'poll': p})
#------------- User ---------------------
if request.user.is_authenticated():
#User objects have two many-to-many fields: models.User. groups and user_permissions. User objects can access their related #objects in the same way as any other Django model:
myuser.groups = [group_list]
myuser.user_permissions = [permission_list]
from django.contrib.auth.decorators import user_passes_test
@user_passes_test(lambda u: u.has_perm('polls.can_vote'))
def my_view(request):
from django.contrib.auth.decorators import permission_required
def my_view(request):
# ...
my_view = permission_required('polls.can_vote', login_url='/loginpage/')(my_view)
#-------------Model--------------------
cheese_blog = Blog.objects.get(name="Cheddar Talk")
Entry.objects.all()
Entry.objects.filter(pub_date__year=2006)
Blog.objects.filter(entry__author__name='Lennon')
>>> b = Blog.objects.get(id=1)
>>> b.entry_set.all() # Returns all Entry objects related to Blog.
#Many to Many
e = Entry.objects.get(id=3)
e.authors.all() # Returns all Author objects for this Entry.
e.authors.count()
e.authors.filter(name__contains='John')
a = Author.objects.get(id=5)
a.entry_set.all() # Returns all Entry objects for this Author.
#----------- Templates -------------
{% if user.is_authenticated %}
{% for group in user.groups.all %}
{{group}}
{% endfor %}
settings.LOGIN_URL.
#-------------Forms-------------------
from django import forms
from django.forms import ModelForm
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
cc_myself = forms.BooleanField(required=False)
f = ContactForm({'subject': 'hello'})
def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form
return render_to_response('contact.html', {
'form': form,
})
#------
<form action="/contact/" method="POST">
{{ form.as_p }}
<input type="submit" value="Submit" />
#------
<form action="/contact/" method="POST">
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_subject">E-mail subject:</label>
{{ form.subject }}
</div>
</form>
#-----
<form action="/contact/" method="POST">
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
#----
<form action="/comments/add/" method="POST">
{% with comment_form as form %}
{% include "form_snippet.html" %}
{% endwith %}
<p><input type="submit" value="Submit comment" /></p>
</form>
#----
class CommentForm(forms.Form):
name = forms.CharField(
widget=forms.TextInput(attrs={'class':'special'}))
url = forms.URLField()
comment = forms.CharField(
widget=forms.TextInput(attrs={'size':'40'}))
#----Model Forms-----
# Create the form class.
class ArticleForm(ModelForm):
class Meta:
model = Article
# Creating a form to add an article.
form = ArticleForm()
# Creating a form to change an existing article.
article = Article.objects.get(pk=1)
form = ArticleForm(instance=article)
# Create a form instance from POST data.
f = ArticleForm(request.POST)
# Save a new Article object from the form's data.
new_article = f.save()
# Create a form to edit an existing Article.
a = Article.objects.get(pk=1)
f = ArticleForm(instance=a).save()
# Create a form to edit an existing Article, but use
# POST data to populate the form.
a = Article.objects.get(pk=1)
f = ArticleForm(request.POST, instance=a)
f.save()
#------
class Musician(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField("Person's first name", max_length=30)
poll = models.ForeignKey(Poll)
toppings = models.ManyToManyField(Topping)
gender = models.CharField(max_length=1, choices=(('M', 'Male'),('F', 'Female'))
class Meta:
ordering = ["horn_length"]
verbose_name_plural = "oxen"
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:DjangoCheatSheet的代碼-創(chuàng)新互聯(lián)
文章來(lái)源:http://www.rwnh.cn/article22/dhhocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站排名、標(biāo)簽優(yōu)化、App開(kāi)發(fā)、企業(yè)建站、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容