简体   繁体   English

Django 可以找到我的静态文件,Pycharm 无法解析它们

[英]Django CAN find my static files, Pycharm CANNOT resolve them

I do not think this is a duplicate.我不认为这是重复的。 I have looked at a lot of answers to similar problems.我看了很多类似问题的答案。

Please note: This runs perfectly fine and django finds all static files.请注意:这运行得很好,django 会找到所有静态文件。

UPDATE: It looks like this is a PyCharm bug.更新:看起来这是一个 PyCharm 错误。 If I move static into my app directory, the PyCharm can resolve it.如果我将 static 移动到我的应用程序目录中,PyCharm 可以解决它。 But, I have this static in the main src dir because multiple apps will be using the CSS.但是,我在主 src 目录中有这个静态,因为多个应用程序将使用 CSS。 Right now it runs and Django has no problem with this.现在它可以运行,Django 对此没有任何问题。 I am thiking this is a PyCharm bug.我认为这是 PyCharm 错误。

PyCharm is set up, and everything else seems to work as far as the Django project goodies that Pycharm offers. PyCharm 已经设置好了,其他一切似乎都可以正常工作,只要 Pycharm 提供的 Django 项目好东西。

Pycharm cannot resolve, whch means that I cannot use auto complete, and it is annoying. Pycharm 无法解析,这意味着我无法使用自动完成功能,很烦人。 If I hit ctrl-space the only directory that pops up is admin.如果我按 ctrl-space,弹出的唯一目录是 admin。 This means that PyCharm is completely ignoring my static directory.这意味着 PyCharm 完全忽略了我的静态目录。

I have also tried making it a templates directory and a resourse director.我还尝试将其设为模板目录和资源主管。 No luck there either.那里也没有运气。

{% load static %} <!-- {% load staticfiles %} -->
...
<link rel="stylesheet" href="{% static ''%}">
<link rel="stylesheet" href="{% static 'css/skeleton.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
...

The code above works fine.上面的代码工作正常。 PyCharm does not find the urls though. PyCharm 没有找到网址。

INSTALLED_APPS = [
    'django.contrib.staticfiles',

    # admin specific
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
]
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

STATIC_URL = '/static/'

Here is the key to solving the problem: Pycharm needs to know where your settings files that you're currently using is located.这是解决问题的关键:Pycharm 需要知道您当前使用的设置文件所在的位置。 See the picture.看图片。

在此处输入图片说明

Also make sure that you have everything setup in your settings file correctly.还要确保您在设置文件中正确设置了所有内容。 See the picture.看图片。

在此处输入图片说明

In some situation this way can work:在某些情况下,这种方式可以工作:

<link href="{{ STATIC_URL }}" rel="stylesheet" type="text/css">

PyCharm 静态自动完成

PyCharm recognises my static files for a configured settings.STATIC_ROOT as below PyCharm 为配置的settings.STATIC_ROOT识别我的静态文件,如下所示

STATIC_URL = '/assets/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Try loading your staticfiles:尝试加载您的静态文件:

{% load staticfiles %}

And setting your link as:并将您的链接设置为:

<link rel="stylesheet" type="text/css" href="{% static "css/default.css" %}">

I used:我用了:

  1. STATIC_ROOT = os.path.join(BASE_DIR, "static_deployment")

  2. python manage.py collectstatic command python manage.py collectstatic命令

So I did what needs to be done to go from development to deployment configuration.所以我做了从开发到部署配置需要做的事情。

pyCharm now works fine and can see static files pyCharm 现在可以正常工作并且可以查看静态文件

Yes it is not the best solution if you change static files a lot but can help developing with pyCharm and Django...是的,如果您经常更改静态文件,这不是最佳解决方案,但可以帮助使用 pyCharm 和 Django 进行开发...

If you have setup everything correctly according to Django doc at: https://docs.djangoproject.com/en/3.1/howto/static-files/如果您已根据 Django 文档正确设置了所有内容: https : //docs.djangoproject.com/en/3.1/howto/static-files/

Then you can try to explicitly set your static folders to help the IDE like:然后,您可以尝试显式设置静态文件夹以帮助 IDE,例如:

STATICFILES_DIRS = (
    BASE_DIR / "app1/static",
    BASE_DIR / "app2/static",
)

this works for me many times.这对我有用很多次。 IntelliJ just could not automatically find my app's static. IntelliJ 只是无法自动找到我的应用程序的静态。

Check in settins.py签入 settins.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'your_app/static/')
]

and add并添加

STATIC_ROOT = os.path.join(BASE_DIR, '/static/')

Pycharm use this settings. Pycharm 使用此设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM