导读:很多朋友问到关于django如何调用views的相关问题,本文首席CTO笔记就来为大家做个详细解答,供大家参考,希望对大家有所帮助!一起来看看吧!
django的views直接传一段html代码调用模板中的js函数。
有三种方法:
1,在模版中的html文件中嵌入含有这个函数的js文件或者直接加到html里面.
2,在js中通过ajax访问打开一个链接,比如/admin/url?key=value,那么在admin.py中的url方法中这样keyvlaue = request.GET.get('key',None)来获取这个value,然后py做一番处理后,直接返回一段嵌有这个js函数的html代码到js中,然后在js中来无刷新的加入到页面中:$("#id").html(xmlhttp.responseText);
3,基本同上,应用到ajax来实现,只是在py一番处理后返回的不是一段实质的html,而是一个msg指令,js在接收到后ajax返回的指令后,运行相关的js函数.比如if (xmlhttp.responseText == 'ok') fun();
回答的希望你能理解.
python django views request多传递个参数
在views里直接导入对应的model,然后调用model里的参数或者方法,需要根据你代码的具体情况灵活变化。
安利一个Django学习网站Django高级教程
python的Django views 文件中引入模块问题
看上面的图片,按你的导包方法,单独运行views是没问题的
但是在django里这样用不行,在运行django项目时,其实当前目录是整个项目的根目录
你可以在views目录下import os,然后 print(os.getcwd())查看当前目录:
打印的当前目录正是整个项目的根目录,我的是E:\mypython\day51...\blw:
你这里的就是G:\....Anno:
你要在Anno目录下导入data_model.py,是不是要带上app名才能进入子级目录对吧?
而且还有个问题就是,在以后的开发中,很有可能在不同app下有相同名字的模块或者方法,比如你这里有三个app,app01,app02,app03,这三个app下都有common_model,那到底按什么区分呢?所以前面最好带上app名。
不过真想方便,可以使用:from .common_model import data_model
【.】代指当前文件的目录,这是一种相对路径的导入
python导入 Django views,无法导入views
看你这样写好像也没有错误呀,是不是view写错了,应该是views才对吧。如果还是不行,那么可以改为 from 应用名 import views试试
学一学书里的django是怎么写views.py的
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect, get_object_or_404
from django.forms.models import modelform_factory
from django.apps import apps
from django.views.generic.base import TemplateResponseMixin, View
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from braces.views import LoginRequiredMixin, PermissionRequiredMixin
from braces.views import CsrfExemptMixin, JsonRequestResponseMixin
from .models import Course, Module, Content
from .forms import ModuleFormSet
from django.db.models import Count
from .models import Subject
from django.views.generic.detail import DetailView
from students.forms import CourseEnrollForm
# Create your views here.
class OwnerMixin(object):
def get_queryset(self):
qs = super(OwnerMixin, self).get_queryset()
return qs.filter(owner=self.request.user)
class OwnerEditMixin(object):
def form_valid(self, form):
form.instance.owner = self.request.user
return super(OwnerEditMixin, self).form_valid(form)
class OwnerCourseMixin(OwnerMixin, LoginRequiredMixin):
model = Course
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
class OwnerCourseEditMinxin(OwnerCourseMixin, OwnerEditMixin):
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
template_name = 'courses/manage/course/form.html'
class ManageCourseListView(OwnerCourseMixin, ListView):
template_name = 'courses/manage/course/list.html'
class CourseCreateView(PermissionRequiredMixin,
OwnerCourseEditMinxin,
CreateView):
permission_required = 'courses.add_course'
class CourseUpdateView(PermissionRequiredMixin,
OwnerCourseEditMinxin,
UpdateView):
permission_required = 'courses.change_course'
class CourseDeleteView(PermissionRequiredMixin,
OwnerCourseMixin,
DeleteView):
template_name = 'courses/manage/course/delete.html'
success_url = reverse_lazy('manage_course_list')
permission_required = 'courses.delete_course'
class CourseModuleUpdateView(TemplateResponseMixin, View):
template_name = 'courses/manage/module/formset.html'
course = None
def get_formset(self, data=None):
return ModuleFormSet(instance=self.course,
data=data)
def dispatch(self, request, pk):
self.course = get_object_or_404(Course, id=pk, owner=request.user)
return super(CourseModuleUpdateView, self).dispatch(request, pk)
def get(self, request, *args, **kwargs):
formset = self.get_formset()
return self.render_to_response({'course': self.course,
'formset': formset})
def post(self, request, *args, **kwargs):
formset = self.get_formset(data=request.POST)
if formset.is_valid():
formset.save()
return redirect('manage_course_list')
return self.render_to_response({'course': self.course,
'formset': formset})
class ContentCreateUpdateView(TemplateResponseMixin, View):
module = None
model = None
obj = None
template_name = 'courses/manage/content/form.html'
def get_model(self, model_name):
if model_name in ['text', 'video', 'image', 'file']:
return apps.get_model(app_label='courses',
model_name=model_name)
return None
def get_form(self, model, *args, **kwargs):
Form = modelform_factory(model, exclude=['owner',
'order',
'created',
'updated'])
return Form(*args, **kwargs)
def dispatch(self, request, module_id, model_name, id=None):
self.module = get_object_or_404(Module,
id=module_id,
course__owner=request.user)
self.model = self.get_model(model_name)
if id:
self.obj = get_object_or_404(self.model, id=id, owner=request.user)
return super(ContentCreateUpdateView, self).dispatch(request, module_id, model_name, id)
def get(self, request, module_id, model_name, id=None):
form = self.get_form(self.model, instance=self.obj)
return self.render_to_response({'form': form, 'object': self.obj})
def post(self, request, module_id, model_name, id=None):
form = self.get_form(self.model,
instance=self.obj,
data=request.POST,
files=request.FILES)
if form.is_valid():
obj = form.save(commit=False)
obj.owner = request.user
obj.save()
print id, ''
if not id:
print '****************'
Content.objects.create(module=self.module,
item=obj)
print '****************'
return redirect('module_content_list', self.module.id)
return self.render_to_response({'form': form, 'object': self.obj})
class ContentDeleteView(View):
def post(self, request, id):
content = get_object_or_404(Content,
id=id,
module__course__owner=request.user)
module = content.module
content.item.delete()
content.delete()
return redirect('module_content_list', module.id)
class ModuleContentListView(TemplateResponseMixin, View):
template_name = 'courses/manage/module/content_list.html'
def get(self, request, module_id):
module = get_object_or_404(Module,
id=module_id,
course__owner=request.user)
return self.render_to_response({'module': module})
class ModuleOrderView(CsrfExemptMixin,
JsonRequestResponseMixin,
View):
def post(self, request):
for id, order in self.request_json.items():
Module.objects.filter(id=id,
course__owner=request.user).update(order=order)
return self.render_json_response({'saved': 'OK'})
class ContentOrderView(CsrfExemptMixin,
JsonRequestResponseMixin,
View):
def post(self, request):
for id, order in self.request_json.items():
Content.objects.filter(id=id,
module__course__owner=request.user)\
.update(order=order)
return self.render_json_response({'saved': 'OK'})
class CourseListView(TemplateResponseMixin, View):
model = Course
template_name = 'courses/course/list.html'
def get(self, request, subject=None):
subjects = Subject.objects.annotate(
total_courses=Count('courses')
)
courses = Course.objects.annotate(
total_modules=Count('modules')
)
if subject:
subject = get_object_or_404(Subject, slug=subject)
courses = courses.filter(subject=subject)
print '***********', subjects, courses, subject
return self.render_to_response({'subjets': subjects,
'subject': subject,
'courses': courses})
class CourseDetailView(DetailView):
model = Course
template_name = 'courses/course/detail.html'
def get_context_data(self, **kwargs):
context = super(CourseDetailView,
self).get_context_data(**kwargs)
context['enroll_form'] = CourseEnrollForm(
initial={'course': self.object})
return context
Django中views如何设置全局变量
问题在于test = 1实际上是定义了一个局部变量test,它隐藏了全局作用域中的test变量。
要指明使用全局的test变量,需要使用global关键字。
from django.http import HttpResponse
test = 0
def a(request):
global test
test = 1
return HttpResponse('view a: test = %d' % test)
def b(request):
global test
test += 1
return HttpResponse('view b: test = %d' % test)
结语:以上就是首席CTO笔记为大家介绍的关于django如何调用views的全部内容了,希望对大家有所帮助,如果你还想了解更多这方面的信息,记得收藏关注本站。