Django Neapolitan not working

As a fairly newbie I was excited about GitHub - carltongibson/neapolitan: Quick CRUD views for Django where as I understand I can get CRUD up and running fast for any new model I make.

I just cant get it to work. Has any of you guys tried it and got it running?

2 Likes

Just to add more context (pun intended :slight_smile: ). I followed the readme on the page and tried to use a model called Bookmark. I put it in an app called Bookmarks.

I added my Bookmark app as well as neapolitan to installed apps (didn’t say on the github that I had to my app the, but I tried it to make it work)

Now for the special part. I can’t read if the code for urls.py is for the project og the app urls-file. I tried both.

I also had a working template called base.html as stated on the page.

No matter what I did I could not make it work.

I hope you guys are willing to give Neapolitan a go and help figure out where I go wrong. Maybe I can even talk Carlton into fool-proofing the readme file for me :smiley:

I got it to work by following the tutorial. The only area where I had a little confusion was for the location of the base.html template. I made it work by creating a templates directory in the project-level dashboard directory and adding that directory to the DIRS setting for TEMPLATES. It will also work if you create the templates directory in projects, and no changes end up being needed to settings.
@carltongibson - Any thoughts on this? Did I misunderstand or miss something?

Regarding the url mods, it’s directly in the tutorial:

And at the end of dashboard/urls.py :

That’s a reference to the project urls file.

1 Like

@qvisty You don’t say what error you’re getting. As long as Neapolitan is added to INSTALLED_APPS and the URLs are added to your URL conf, you should get something — even if it’s a template not found error.

  • URLs should be added to the root URL configuration.
  • Assuming default settings, a templates folder in your app will be detected, or as Ken says, you can add a project level one. (This is so taste-specific that I’m somewhat hesitant to make a more concrete recommendation. What I do is not necessarily standard).
1 Like

Thank you both. I will try agsin tonight (office hours :grin:) and comment here how it goes. :slightly_smiling_face:

Here is what I have done:

ran
pip install neapolitan

Add neapolitan to my INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "neapolitan",
]

Made a project called Bookmarks
Trying to make a single folder project like Carltons link above, so I added admin.py, views.py and forms.py)
registered Bookmarks in settings file

INSTALLED_APPS = [
    ...   
    "Bookmarks",
]

changed to this in settings:

TEMPLATES = [
    {
        ...
        "DIRS": [ BASE_DIR / "templates", ],
        ...
    },
]

put base.html in templates folder with:
{% block content %}{% endblock %}

Made the django model Bookmarks as per the Neapolitan guide

from django.db import models

class Bookmark(models.Model):
    url = models.URLField(unique=True)
    title = models.CharField(max_length=255)
    note = models.TextField(blank=True)
    favourite = models.BooleanField(default=False)

in the project urls file added this at the bottom

# urls.py
from neapolitan.views import CRUDView

class BookmarkView(CRUDView):
    model = Bookmark
    fields = ["url", "title", "note"]
    filterset_fields = [
        "favourite",
    ]

urlpatterns = [ ... ] + BookmarkView.get_urls()

so now my whole urls.py looks like this:

from django.contrib import admin
from django.urls import path

# urls.py
from neapolitan.views import CRUDView


class BookmarkView(CRUDView):
    model = Bookmark
    fields = ["url", "title", "note"]
    filterset_fields = [
        "favourite",
    ]


urlpatterns = [
    path("admin/", admin.site.urls),
] + BookmarkView.get_urls()

(I am wondering why it does not import the model?)

added this to admin.py

from .models import Bookmark

admin.site.register(Bookmark)

ran
python manage.py makemigrations with this error:
ModuleNotFoundError: No module named 'neapolitan'

Now I tried following this instead: LINK

I got past the makemigrations, which worked here.

But after I did:
And at the end of dashboard/urls.py:

from neapolitan.views import CRUDView

import projects

class ProjectView(CRUDView):
    model = projects.models.Project
    fields = ["name", "owner", "last_review", "has_tests", "has_docs", "status"]

urlpatterns += ProjectView.get_urls()

I got this error:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\core\management\commands\runserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\core\management\base.py", line 485, in check
    all_issues = checks.run_checks(
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    return check_method()
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\urls\resolvers.py", line 494, in check
    for pattern in self.url_patterns:
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\dashboard\dashboard\urls.py", line 25, in <module>
    from neapolitan.views import CRUDView
  File "C:\Users\Jesper\Documents\jq Code\neapolitantutorial\venv\lib\site-packages\neapolitan\views.py", line 28
    match self:
          ^
SyntaxError: invalid syntax

I now tried moving the templates folder, som I tried it as both a project level templates folder and an app level templates folder. Same error.

I am now stuck on both tries :smiley:

Neapolitan uses match statement introduced in python 3.10.

If you get such syntax error, it is because your python version is lower than that.

2 Likes

Yes, I need to add a python requires declaration to the metadata here, but Python 3.10 is needed.

1 Like

This was the answer. Thank you :slight_smile: (I thought I had installed 3.12, but it was still using 3.8.
Maybe someone else will find this thread and find it helpful.

@carltongibson. Where do you prefer I post “how do I…”-questions about using Neapolitan? Thank you for a great package. It will save me heaps of time.
Just to try it out i added an extra model and copy-pasted a few lines of code in admin.py and urls.py and the new functioning model-CRUD was flying. This is great! :slight_smile:

@qvisty Glad you’re up and running! :rocket:

Here is fine for usage questions if you like. (I need to find a way to alert matching “Neapolitan” :thinking:)

If you find an issue, or have suggestions to make the docs better(!) the repo is more suitable though.

1 Like