obj=models.UserInfo.objects.filter(level = "管理员")
时间: 2024-05-25 14:15:42 浏览: 18
This code is querying the UserInfo model in Django for all instances where the level field equals "管理员". It will return a QuerySet object containing all matching instances.
相关问题
try: Publisher_obj = models.Publisher.objects.all() Author_obj = models.Author.objects.all() BookType_obj = models.BookType.objects.all() if request.method == 'POST': back_dic = {"code": 200, 'msg': ''} bookface = request.FILES.get('bookface') book_obj = dict(request.POST) print(book_obj) price = Decimal(request.POST.get('price')) ISBN = request.POST.get('ISBN') obj = models.Book.objects.filter(ISBN=ISBN).first() if not obj: book = models.Book.objects.create(name=book_obj['name'], face=bookface, ISBN=book_obj['ISBN'], price=price, pub_date=book_obj['pub_date'], book_number=book_obj['number'], content=0, info=book_obj['info']) book.book_type.set(book_obj['booktype']) book.author.set(book_obj['author']) book.publisher.set(book_obj['publisher']) book.save() back_dic['url'] = 'admin_book' else: back_dic['code'] = 303 back_dic['msg'] = '错误' return JsonResponse(back_dic) return render(request, 'admin_book_add.html', locals()) except Exception: traceback.print_exc()
在这段代码中,如果出现了异常,它并没有返回一个 `HttpResponse` 对象,而是直接打印了异常信息。这可能会导致在发生异常时,返回一个空值(`None`),从而引发 `ValueError` 错误。
为了解决这个问题,你可以在 `except` 代码块中添加一个返回语句,返回一个 `HttpResponse` 对象,例如:
```
except Exception as e:
traceback.print_exc()
return HttpResponse('An error occurred: {}'.format(e))
```
这样,当出现异常时,它将返回一个包含异常信息的 `HttpResponse` 对象,而不是空值(`None`)。当然,你可以根据你的具体需求来定制返回的内容。
希望这能帮助你解决问题。
def addcircular(request): if request.method == 'POST': content = request.POST.get('content') username = request.session.get('username') user_obj = models.User.objects.get(username=username) user_id = user_obj.pk models.CompanyNotice.objects.create(content=content, user_id=user_id) return redirect('/user/showcircular') return render(request, 'notice/addcircular.html')
这是一个 Django 的视图函数,用于处理添加公司公告的请求。当请求的方法为 POST 时,从请求的数据中获取公告的内容和当前用户的用户名,然后查询数据库获取当前用户的 ID,最后创建一条新的公司公告,将公告内容和用户 ID 存入数据库。最后重定向到显示公司公告的页面。如果请求的方法不是 POST,就渲染添加公司公告的页面。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)