Help with Django Neapolitan

Hi guys. I need a bit of help here. I am using Neapolitan to get a quick start of my CRUD for each model. So far loving it for getting of the ground fast.

I am having a bit of trouble though, and I hope some of you have solved it.

I am now at the point where I want to write my own list-view for one of the models, but still use the neapolitan-view for create, detail and delete.

I can’t figure out how to do that as I don’t know how to “tie it together - half mine, half Neapolitan”.

I hope you know what I am trying to do and can help me move forwards

What is the specific issue that you are facing?

The Neapolitan urls are added to your urlpatterns definition, this means that you still have the ability to use any other views you want. There’s nothing you do within it that would inhibit you from doing anything else you normally do in a Django project.

Or are you looking to integrate your view within the CRUDView object? In that case, then your list view becomes the list function within that object.

(Sorry, it’s not clear to me what it is you’re trying to do or where you’re finding difficulty doing it.)

I think I had a hard time explaining exactly what I want it to do.

I think my first question goes like this. Imagine I have a model Book where I am using Neapolitan CRUDView. I have a template for showing the list of books (object-list or book-list) where the View Edit Delete links are shown. I want to write my own listview and template, but in the template I cant figure out how to still keep using the {% url ??? ??? %} for the Neapolitan urls still using the Neapolitan templates

I have seen, that Neapolitan even provides the possibility to copy the template with my model, but the template code where it uses role=… is beyond me.

The CRUDView class has that get_urls method that returns the urlpatterns for that class.

So if your CRUDView class for your Book model is BookView, then BookView.get_urls() returns the list of generated URLs.

If you want just a specific URL, you can use the roles parameter in the get_urls call, but you need to import the Role class from neapolitan.views.

In other words, working from the Neapolitan tutorial where the model is named Project and the CRUDView class is named ProjectView, then to get the url name for use in either the reverse function or the url tag would be a sequence of code that looks like this:

from dashboard.urls import ProjectView
from neapolitan.views import Role

detail_url_name = ProjectView.get_urls(roles={Role.DETAIL})[0].name

Or, knowing that the url names are generated as modelname-suffix (unless you’ve set the url_base attribute on the CRUDView class), and that the suffixes are ['-list', '-create', '-detail', '-update', '-delete'] you could construct the names directly yourself.

In other words, you know that the urls for your Book model are going to be 'book-list', 'book-create', 'book-detail', 'book-update', and 'book-delete'. There’s no real need for you to go through the process above to retrieve the url names.

Which are the names I use in the url tag, right?

And I pass book.id as per usual in the url too, right?

You are correct, yes to both.

1 Like

Thank you so much :smiling_face: