While running `Django manage.py test`, the `urls.py` file was not visible, even though it is detected

I have a django project structured like this:

Base Directory
| manage.py
| configDir
 \
  | settings.py , urls.py, wsgi.py
|mainApp
 \ 
  |urls.py , views, models etc.
  | tests
  \
   |urlTest.py

(To clarify, there is a django config directory and a mainApp directory. The mainApp has its own urls.py and the tests directory. All directories are python modules with an init.py)

When I run

manage.py runserver

The application runs as expected on the django server.

However, when running

manage.py test

I encounter the following error:

django.urls.exceptions.NoReverseMatch: Reverse for 'index' not found. 'index' is not a valid view function or pattern name.

The test code snippet:

[...]
class TestSth(BaseTest):
def setUp(self):
    [some database setup such as users etc]

def test_get(self):
    pprint.pprint(reverse('index'))

config/urls.py contains:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
     url(r'^admin/', admin.site.urls),
     url(r'^mainApp/', include('mainApp.urls')),
]

mainApp/urls.py includes:

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
app_name = 'app'

urlpatterns = [
    #/app/
    url(r'^$', views.IndexView.as_view(), name='index'),
    [...]

If anyone can suggest what might be done wrong here, I would greatly appreciate it. This seems puzzling to me.

Answer №1

In the mainApp/urls.py file, an app_name has been declared, so make sure to reference that in the reverse call:

pprint.pprint(reverse('app:index'))

Answer №2

The NoReverseMatch exception occurs within the realm of django.urls when there is no match found for a URL in your URLconf that matches the supplied parameters.

It is essential to include the urlConf parameter when using the reverse function.

reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)

As per Django official documentation:

The urlconf argument refers to the URLconf module containing the URL patterns that are utilized for reversing. Typically, the root URLconf for the ongoing thread will be used by default.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to conduct classification using a float data type post normalization?

Currently, I am working on analyzing a dataset named Adult and attempting to execute a KNN (K Nearest Neighbors) algorithm on certain columns in a new dataframe that I have created. A few of these columns have been normalized. However, during the process, ...

Display various options for administrators in Django

I need to allow the admin to select multiple choices at once, using checkboxes. However, when I tried implementing this, it displayed a dropdown list instead of checkboxes. Here is the code snippet: models.py class segmentation_Rules(models.Model): ...

Finding elements using Selenium in Python

The task at hand is to click on numbers 1 through 50 in sequence Number Elements: 1 - <span class="box" style="z-index:99"></span> 2 - <span class="box" style="z-index:98"></span> 3 - <sp ...

How to fetch a list of users in an organization from Azure DevOps using Python API call

I'm attempting to create a basic api call in Python to Azure DevOps to retrieve a list of users. The URL is returning results when accessed through a browser, but I am encountering an error while scripting it as shown below. I need assistance in prope ...

What is the most effective way to iterate over a list of strings and utilize them in a condition statement?

source = open("file1") out = open("file2", "w") months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'] for line in source: out.write(line) if line.startswith('REPL ...

Using Python Pandas Panel Data to Replace Missing Values with Data from Previous or Subsequent Periods

I have a data set containing panel data, with observations of units over multiple time periods. For instance: dates = 3 * list(pd.date_range(start='1/31/2018', end='3/31/2018', freq="M")) unit_id = ["id_1", "id ...

What is preventing sinon from substituting the real function invocation?

Here is the function I am working with: const PhoneNumber = require('awesome-phonenumber'); const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN); const { twiml } = require('twilio'); ...

running a shell script using nohup commands through Python scripts

Hello everyone, I am a beginner in Python scripting and I have encountered an issue while trying to execute a JBoss startup shell script through Python. The shell script runs perfectly fine when executed directly, but it seems to encounter problems when ru ...

I am interested in utilizing Python to separate the URL according to the instructions provided in the description

URL = ftp://0.0.0.0/Engineering/Camera_Services/xxxxxx.x/QA_Release/zzzzzz_11-MAR-2022.zip I need to separate and assign them to variables as shown below Expected Output: Custom Folder = Engineering/Camera_Services/xxxxxx.x/QA_Release Filename = zzzzzz_1 ...

Adjusting the width of plots in Jupyter notebookIs that good

In my current project, I have created two plots: I would like to make them uniform in width. Is there a way to achieve this in ipython notebook while using %matplotlib inline? UPDATE: These plots are generated using the following functions: import nump ...

Tips for combining variously shaped convolutional layer outputs into a consistent shape for inputting into a Fully Connected layer

I am facing a challenge with processing input images of different sizes in a Convolutional Neural Network (CNN). After passing the images through Conv layers, I need to connect the outputs to a Fully Connected Layer for classification. For vectorization p ...

Python and Selenium error message: Cannot find module named 'org'

Currently, I'm experimenting with the Select function in Selenium for Python 3 to assist with interacting with drop down menus. Nevertheless, I encounter an issue when attempting to import org.openqa.selenium.support.ui.Select as it presents me with t ...

What is the reason behind the lack of falseness in Python Queue?

When working with Python, I have become accustomed to container objects exhibiting truthy behavior when they are populated and falsey behavior when they are empty. This pattern is common in lists, deques, and other data types: # list a = [] not a True a. ...

Transform JSON data into a Pandas DataFrame

Is there a way to extract the employees' data from this JSON and convert it into a Python dataframe, excluding unnecessary fields? {'fields': [{'id': 'displayName', 'type': 'text', 'name': ...

Reordering numbers within a list depending on specified criteria

There is a program designed to rearrange numbers in a list based on a pivot value. The requirement is that all numbers before the pivot should be less than or equal to it, while all numbers after the pivot should be greater than it. This script is written ...

The occurrence of a WSGI/Nginx/Internal server error occurs when the virtual environment is disabled due to the absence of a python

Having trouble with an error message that says, "-— no python application found, check your startup logs for errors —- Internal server error." Everything works fine when I'm in a virtual environment, but as soon as I deactivate it, I keep encount ...

Python Enum using an Array

I am interested in implementing an enum-based solution to retrieve an array associated with each enum item. For example, if I want to define a specific range for different types of targets, it might look like this: from enum import Enum class TargetRange ...

Boolean Filtering in DataFrameThe process of filtering boolean values within a

Looking for a way to filter rows in a dataframe that have 2 to 4 True values present? Consider the following example dataframe: A B C D E F 1 True True False False True 2 False True True True False 3 False False False Fal ...

Multiplication in list comprehension may yield unexpected results

from math import sin def euler_algorithm(f, initial_condition, initial_time, step_size, num_steps): current_time = initial_time current_state = initial_condition while current_time <= num_steps: current_time += step_size ...

Implementing SIFT algorithm in Python version 3.4

Currently, I am running Python 3.4 with Anaconda 2.3.0 on MacOS to experiment with the SIFT algorithm. However, whenever I try to execute my code, I keep receiving a "command not found" error. I have included Vlfeat 0.9.20 sift and the necessary library fi ...