我將我的 Django 專案遷移到了新的生產服務器。在以前的生產服務器上,一切正常。遷移時,我升級到了 Ubuntu 20.04 和 Django 4.0.3。現在一切都恢復正常了,除了上傳檔案。
當我嘗試創建 Invoice 物件的實體時,它可以作業,只要我不嘗試將檔案與它一起上傳。添加帶有檔案的發票會出現以下錯誤:
Internal Server Error: /stock/create_invoice
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/html/stock/views.py", line 346, in create_invoice
invoice.save()
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 806, in save
self.save_base(
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 857, in save_base
updated = self._save_table(
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 1000, in _save_table
results = self._do_insert(
File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 1041, in _do_insert
return manager._insert(
File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 1434, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1620, in execute_sql
for sql, params in self.as_sql():
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1547, in as_sql
value_rows = [
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1548, in <listcomp>
[
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1549, in <listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1497, in pre_save_val
return field.pre_save(obj, add=True)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/fields/files.py", line 316, in pre_save
file.save(file.name, file.file, save=False)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/fields/files.py", line 92, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/usr/local/lib/python3.8/dist-packages/django/core/files/storage.py", line 57, in save
name = self._save(name, content)
File "/usr/local/lib/python3.8/dist-packages/django/core/files/storage.py", line 339, in _save
os.chmod(full_path, self.file_permissions_mode)
PermissionError: [Errno 1] Operation not permitted: '/var/www/html/media/invoices/veryniceinvoice.pdf'
Internal Server Error: /stock/invoice/0/
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/var/www/html/stock/views.py", line 159, in invoice
invoice = Invoice.objects.get(pk=invoice_id)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 496, in get
raise self.model.DoesNotExist(
stock.models.Invoice.DoesNotExist: Invoice matching query does not exist.
該檔案已上傳到正確的(cifs 掛載)檔案夾,但由于某種原因,無法將包含檔案鏈接的物件實體添加到資料庫中?
我想問題是某種許可問題。我嘗試通過將所有內容設定為 777 來解決這個問題,所以我至少可以看看這是否解決了問題,但事實并非如此。/var/www/html/media 的檔案夾權限目前為:
drwxrwxrwx 2 root root 0 Mar 17 08:57 invoices
chown doesn't work for some reason (I read about this having something to do with the cifs mount?), but var-www wasn't the owner of the invoices folder on the previous server either, and that worked fine...
Any help would be very much appreciated!
uj5u.com熱心網友回復:
我找到了解決方案。
我必須將uid=www-data,forceuid 添加到 cifs mount 命令。apache 用戶沒有正確的權限。
(然后卸載,再次安裝)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446986.html
