Django開発のいくつかの技術的ポイント(クラスビュー、django-taggit、markdownx)

Django

1.クラスビュー(CBV):

https://www.cnblogs.com/chichung/p/9886655.html

https://blog.csdn.net/AI_GG/article/details/103696360

https://www.cnblogs.com/knighterrant/p/10503645.html

https://blog.csdn.net/xujin0/article/details/84038778

https://blog.csdn.net/xujin0/article/details/84038778

https://www.runoob.com/django/django-form-component.html

  1. バックエンドロジック処理では、リクエストメソッドがgetリクエストかPOSTリクエストかを判断するロジックは使用されません。ビュークラスでは、getメソッドの定義はgetリクエストを書き込むロジックであり、クラスの定義です。 postメソッドはpostリクエストロジックです。

  2. 一般的な実行フローを表示する

    self.as_view()-----> self.dispatch()------> self.get / post

    工順配布は、特定の要求アスペクト名の取得を開始し、特定の要求機能ビュー機能を反映します。

    class View:
    	http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
    
    	def as_view(cls, **initkwargs):
    
    		def view(request, *args, **kwargs):
    
    			return self.dispatch(request, *args, **kwargs)
    		return view
    
    	def dispatch(self, request, *args, **kwargs):
    		if request.method.lower() in self.http_method_names:
    			#用反射获取函数
    			handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
    		else:
    			handler = self.http_method_not_allowed
    
    		return handler(request, *args, **kwargs)
    
    
    from django.view import View
    
    class LoginView(View):
    	def get(self, request, *args, **kwargs):
    		return render(request, 'login.html')
    
    
    	def post(self, request, *args, **kwargs):
    		return redirect('/index/')
    

    クラスビューの利点:

    優れたコード可読性

    クラスビューは関数よりも再利用性が高く、クラスビューは関数を実装します。クラスビューの特定のロジックが他の場所で必要な場合は、このクラスビューを直接継承するだけで十分です。

2.その他の問題

アクションの問題:https//blog.csdn.net/qq_34045989/article/details/88977104

写真のアップロード:https//blog.csdn.net/meylovezn/article/details/47124923

MEDIA_ROOT:https://segmentfault.com/a/1190000021960355

https://www.cnblogs.com/icat-510/p/9034727.html


django-taggit:https://cloud.tencent.com/developer/article/1635695

In [4]: from articles.models import TestImage

In [5]: ti  =TestImage.objects.create(name='yyt', img='img/test.png')

In [6]: ti.tags.add('new','sad','angry')

In [7]: ti.tags.all()
Out[7]: <QuerySet [<Tag: angry>, <Tag: new>, <Tag: sad>]>

In [8]: TestImage.objects.filter(tags__name__in=['new'])
Out[8]: <QuerySet [<TestImage: TestImage object (13)>]>

django-taggitドキュメント:https://django-taggit.readthedocs.io/en/latest/api.html


django-markdownの実装:https//blog.csdn.net/Empire_03/article/details/87112250

使用マークダウン:https//blog.csdn.net/qq_34405401/article/details/102528305

http://www.zhidaow.com/post/djaog-1-6-markdown

pip install markdown

[外部リンクの画像転送に失敗しました。ソースサイトにアンチホットリンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-5XNet2oz-1599186913338)(C:\ Users \ 93623 \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200813094512583.png)]

マークダウンをdjangoに埋め込む基本的な考え方は、ページにボックスを定義してマークダウン文法を書き込むだけで、書き込んだ後、マークダウン文法をhtmlステートメントに処理してページ上で実行できます。一般的には、テンプレート。レンダリングのmarkdwonサポートを追加

django-markdown-deux:https//github.com/trentm/django-markdown-deux

エラー:?:( staticfiles.E002)STATICFILES_DIRS設定にSTATIC_ROOT設定を含めることはできません

[外部リンクの画像転送に失敗しました。ソースサイトにアンチホットリンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-fn6dZiwj-1599186913341)(C:\ Users \ 93623 \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200817135301311.png)]

https://blog.csdn.net/Sun_White_Boy/article/details/80684620

[外部リンクの画像転送に失敗しました。ソースサイトにアンチホットリンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-TXRv2mxi-1599186913342)(C:\ Users \ 93623 \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200817135515055.png)]


ルーティングフォーマットをurls.pyに書き込む
ここに画像の説明を挿入


djangoフォーム送信アクションのURLを書く方法は?

[外部リンクの画像転送に失敗しました。ソースサイトにアンチホットリンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-8M5SgdUu-1599186913345)(C:\ Users \ 93623 \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200817144636789.png)]


次のように記述されたフォームでテキストフィールドタイプのデータを定義する方法forms.CharField(widget=forms.Textarea)


class BlogForm(forms.Form):
		title  = forms.CharField(required = True) 
		content = forms.CharField(widget=forms.Textarea) 

おすすめ

転載: blog.csdn.net/weixin_46129834/article/details/108399557