Django开发的一些技术点(类视图,django-taggit,markdownx)

Django

一. 类视图(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. View大致的执行流程

    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/')
    

    类视图的好处:

    代码可读性好

    类视图相对于函数有更高的复用性,一个类视图实现一块功能,如果其他地方需要某个类视图的某个特定的额逻辑,直接继承这个类视图就可以了

二. 其它问题

action 问题: 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

使用markdown: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)]

将markdown嵌入到django中的基本思路是:随便在一个页面中定义一个框书写markdown语法,然后写完后将markdown语法经过处理变成html语句传到页面中执行就可以,总体来说就是为模板渲染加入markdwon支持

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

ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT settin

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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 form表单提交action的url怎么写?

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8M5SgdUu-1599186913345)(C:\Users\93623\AppData\Roaming\Typora\typora-user-images\image-20200817144636789.png)]


在forms里面怎么定义textfield类型的数据,写成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