I'm currently working on modifying an url.py file in django project using RedBaron. This file already contains two FromImportNode nodes. I'm attempting to add a new import statement (from django.conf.urls.static import static) to these existing nodes.
Additionally, I need to insert this new import before the urlpatterns = [...] statement within the file.
However, despite successfully adding the new import to the FromImportNode nodes, I'm facing challenges when trying to save these changes using file.write. Strangely, other modifications, such as inserting Count_urls, are being saved correctly.
red.append('\n')
with open(url_path, 'w') as file:
file.write(red.dumps())
Could you kindly assist me in understanding how to properly add the new import to the existing FromImportNode, insert it before urlpatterns, and ensure that these modifications are successfully saved using RedBaron's functionality?
Thank you very much for your help.
my code :
def modify_urls_App(url_path):
with open(url_path, 'r') as file:
code = file.read()
red = RedBaron(code)
#import to add
new_import = RedBaron("from django.conf.urls.static import static")
# FromImportNode nodes
from_import_nodes = red.find_all('FromImportNode')
# adding new_import not saved to red
if from_import_nodes:
from_import_nodes.append(new_import[0])
print(from_import_nodes)
this code is workins
Count_urls = red.find("assignment", target=lambda x: x.dumps() == "urlpatterns")
Count_urls.value.append(f'path("accounts/", include("django.contrib.auth.urls")),')
with open(url_path, 'w') as file:
file.write(red.dumps())
Dear RedBaron Support,
I'm currently working on modifying an url.py file in django project using RedBaron. This file already contains two FromImportNode nodes. I'm attempting to add a new import statement (from django.conf.urls.static import static) to these existing nodes.
Additionally, I need to insert this new import before the urlpatterns = [...] statement within the file.
However, despite successfully adding the new import to the FromImportNode nodes, I'm facing challenges when trying to save these changes using file.write. Strangely, other modifications, such as inserting Count_urls, are being saved correctly.
like : Count_urls = red.find("assignment", target=lambda x: x.dumps() == "urlpatterns") Count_urls.value.append(f'path("accounts/", include("django.contrib.auth.urls")),')
Could you kindly assist me in understanding how to properly add the new import to the existing FromImportNode, insert it before urlpatterns, and ensure that these modifications are successfully saved using RedBaron's functionality?
Thank you very much for your help.
my code :
def modify_urls_App(url_path):
this code is workins