我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

DjangoCheatSheet的代码-创新互联

将写代码过程中较好的代码做个记录,下面代码段是关于Django CheatSheet的代码,希望能对大家也有帮助。

创新互联是一家专注于成都网站制作、成都网站建设与策划设计,济南网站建设哪家好?创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:济南等地区。济南做网站价格咨询:028-86922220
from 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.as_p }} #------
{{ form.subject.errors }} {{ form.subject }}
#-----
{% for field in form %}
{{ field.errors }} {{ field.label_tag }}: {{ field }}
{% endfor %}

#----
{% with comment_form as form %} {% include "form_snippet.html" %} {% endwith %}

#---- 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"

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:DjangoCheatSheet的代码-创新互联
文章源于:http://shouzuofang.com/article/dhhocc.html

其他资讯