[Python] Django CheatSheet →→→→→进入此内容的聊天室

来自 , 2020-12-01, 写在 Python, 查看 128 次.
URL http://www.code666.cn/view/4928e751
  1. from django.http import HttpResponseRedirect
  2. from django.shortcuts import render_to_response, get_object_or_404
  3. from django.contrib.auth.models import User
  4. from django.http import HttpRequest
  5. from django.conf import settings
  6.  
  7.  
  8. request.GET.keys()
  9. return render_to_response('content/auktionen/erstellen/wizard/zusammenfassung.html', locals())
  10. return render('content/auktionen/erstellen/cc_wizard/zusammenfassung.html', locals(), request)
  11. return HttpResponseRedirect('/login/?next=%s' % request.path)
  12. request.GET.get('q', '')
  13. username = request.POST['username']
  14. request.method == 'POST'
  15. HttpRequest.is_ajax()
  16.  
  17. p = get_object_or_404(Poll, pk=poll_id)
  18. get_list_or_404()
  19. return render_to_response('polls/detail.html', {'poll': p})
  20. #------------- User ---------------------
  21. if request.user.is_authenticated():
  22.  
  23. #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:
  24. myuser.groups = [group_list]
  25. myuser.user_permissions = [permission_list]
  26.  
  27. from django.contrib.auth.decorators import user_passes_test
  28.  
  29. @user_passes_test(lambda u: u.has_perm('polls.can_vote'))
  30. def my_view(request):
  31.  
  32. from django.contrib.auth.decorators import permission_required
  33. def my_view(request):
  34.     # ...
  35. my_view = permission_required('polls.can_vote', login_url='/loginpage/')(my_view)
  36.  
  37. #-------------Model--------------------
  38. cheese_blog = Blog.objects.get(name="Cheddar Talk")
  39. Entry.objects.all()
  40. Entry.objects.filter(pub_date__year=2006)
  41. Blog.objects.filter(entry__author__name='Lennon')
  42.  
  43. >>> b = Blog.objects.get(id=1)
  44. >>> b.entry_set.all() # Returns all Entry objects related to Blog.
  45.  
  46. #Many to Many
  47. e = Entry.objects.get(id=3)
  48. e.authors.all() # Returns all Author objects for this Entry.
  49. e.authors.count()
  50. e.authors.filter(name__contains='John')
  51. a = Author.objects.get(id=5)
  52. a.entry_set.all() # Returns all Entry objects for this Author.
  53. #----------- Templates -------------
  54.  
  55. {% if user.is_authenticated %}
  56. {% for group in user.groups.all %}
  57.  {{group}}
  58. {% endfor %}
  59.  
  60. settings.LOGIN_URL.
  61.  
  62. #-------------Forms-------------------
  63. from django import forms
  64. from django.forms import ModelForm
  65.  
  66. class ContactForm(forms.Form):
  67.     subject = forms.CharField(max_length=100)
  68.     message = forms.CharField()
  69.     cc_myself = forms.BooleanField(required=False)
  70.  
  71. f = ContactForm({'subject': 'hello'})
  72.  
  73. def contact(request):
  74.     if request.method == 'POST': # If the form has been submitted...
  75.         form = ContactForm(request.POST) # A form bound to the POST data
  76.         if form.is_valid(): # All validation rules pass
  77.             # Process the data in form.cleaned_data
  78.             # ...
  79.             return HttpResponseRedirect('/thanks/') # Redirect after POST
  80.     else:
  81.         form = ContactForm() # An unbound form
  82.  
  83.     return render_to_response('contact.html', {
  84.         'form': form,
  85.     })
  86.  
  87. #------
  88. <form action="/contact/" method="POST">
  89. {{ form.as_p }}
  90. <input type="submit" value="Submit" />
  91.  
  92. #------
  93. <form action="/contact/" method="POST">
  94.     <div class="fieldWrapper">
  95.         {{ form.subject.errors }}
  96.         <label for="id_subject">E-mail subject:</label>
  97.         {{ form.subject }}
  98.     </div>
  99. </form>
  100. #-----
  101. <form action="/contact/" method="POST">
  102.     {% for field in form %}
  103.         <div class="fieldWrapper">
  104.             {{ field.errors }}
  105.             {{ field.label_tag }}: {{ field }}
  106.         </div>
  107.     {% endfor %}
  108.     <p><input type="submit" value="Send message" /></p>
  109. </form>
  110. #----
  111. <form action="/comments/add/" method="POST">
  112.     {% with comment_form as form %}
  113.         {% include "form_snippet.html" %}
  114.     {% endwith %}
  115.     <p><input type="submit" value="Submit comment" /></p>
  116. </form>
  117. #----
  118. class CommentForm(forms.Form):
  119.     name = forms.CharField(
  120.                 widget=forms.TextInput(attrs={'class':'special'}))
  121.     url = forms.URLField()
  122.     comment = forms.CharField(
  123.                widget=forms.TextInput(attrs={'size':'40'}))
  124. #----Model Forms-----
  125. # Create the form class.
  126. class ArticleForm(ModelForm):
  127.     class Meta:
  128.         model = Article
  129.  
  130.  
  131. # Creating a form to add an article.
  132. form = ArticleForm()
  133. # Creating a form to change an existing article.
  134. article = Article.objects.get(pk=1)
  135. form = ArticleForm(instance=article)
  136.  
  137. # Create a form instance from POST data.
  138. f = ArticleForm(request.POST)
  139.  
  140. # Save a new Article object from the form's data.
  141. new_article = f.save()
  142.  
  143. # Create a form to edit an existing Article.
  144. a = Article.objects.get(pk=1)
  145. f = ArticleForm(instance=a).save()
  146.  
  147. # Create a form to edit an existing Article, but use
  148. # POST data to populate the form.
  149. a = Article.objects.get(pk=1)
  150. f = ArticleForm(request.POST, instance=a)
  151. f.save()
  152. #------
  153. class Musician(models.Model):
  154.   id = models.AutoField(primary_key=True)
  155.   first_name = models.CharField("Person's first name", max_length=30)
  156.   poll = models.ForeignKey(Poll)
  157.   toppings = models.ManyToManyField(Topping)
  158.   gender = models.CharField(max_length=1, choices=(('M', 'Male'),('F', 'Female'))
  159.   class Meta:
  160.         ordering = ["horn_length"]
  161.         verbose_name_plural = "oxen"
  162. #//python/3961

回复 "Django CheatSheet"

这儿你可以回复上面这条便签

captcha