0

I´m trying to make a cron inside my django app with django-crontab==0.7.1,. Within my project settings installed apps, crontab app first, and then my app, both registered. In my cron.py:

from .models import Url

urls=Url.objects.values_list('url')

def foo(url):
    if url not in urls:
        link= Url(
            url=url,
        )
        link.save()
foo('https://example.org')

Also tried to put inside cron.py, but it doesn´t make sense since both are inside same app

from foo.models import Url

Also init.py inside app

My models.py inside foo app, after migrations ran

from django.db import models

class Url(models.Model):
    url=models.URLField('url',blank=False,null=False)

but still getting ModuleNotFoundError: No module named 'foo' Sorry if this has been asked before, but nothing watched yet gives me the answer

Thank you

Vidal M.
  • 133
  • 1
  • 11
  • Could you comment on how you are running your code? – Brian61354270 Dec 07 '21 at 19:33
  • @Brian, I'm running It from console, since I'm on Windows, Hoping cron works then just as fine as console, is that a problem? – Vidal M. Dec 07 '21 at 19:56
  • If you're running `cron.py` directly as a top-level script (e.g. via `python3 foo/cron.py`), then it won't be aware of the package structure, so relative import won't work. For that you'll need to run it as a submodule of the `foo` package via `python3 -m foo.cron`. – Brian61354270 Dec 07 '21 at 20:09
  • See also [Relative imports for the billionth time](https://stackoverflow.com/q/14132789/11082165) – Brian61354270 Dec 07 '21 at 20:09
  • If I do that, i get django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. For that solved with import os,django os.environ.setdefault ("DJANGO_SETTINGS_MODULE", "base.settings") django.setup(). But still have the doubt if cron will do the work, as I´m copletely lost on linux, crons, and bash – Vidal M. Dec 07 '21 at 20:33
  • Your project has a apps folder to place the apps our everything is the same level as manage.py? – Luiz Dec 07 '21 at 21:06
  • Please show your **`settings.py`** – Lord Elrond Dec 07 '21 at 21:07

0 Answers0