如何将第三方应用的网址整合到我的网址结构中?

时间:2016-08-31 14:10:23

标签: django django-urls

我想使用第三方应用,例如imagestore。 默认imagestore url config的作用类似于site.com/gallery/(album, image, tag)。我想将网址更改为site.com/username/(album, image, tag)(即删除gallery),但不知道该怎么做,因为namespace='imagestore' imagestore使用了内部。

# core urls
urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='base.html'), name="home"),
    url(r'^gallery/', include('imagestore.urls', namespace='imagestore')),
    url(r'^(?P<user_url>[\w.-]+)/', include('profiles.urls', namespace='profiles_username')),
]

# profiles.urls
from . import views

urlpatterns = [
    url(r'^$', views.profiles_home, name='profiles_home'),
]

# piece of imagestore urls
urlpatterns = [
    url(r'^$', AlbumListView.as_view(), name='index'),

    url(r'^album/add/$', CreateAlbum.as_view(), name='create-album'),
    url(r'^album/(?P<album_id>\d+)/$', ImageListView.as_view(), name='album'),
    url(r'^album/(?P<pk>\d+)/edit/$', UpdateAlbum.as_view(), name='update-album'),
    url(r'^album/(?P<pk>\d+)/delete/$', DeleteAlbum.as_view(), name='delete-album'),

    url(r'^tag/(?P<tag>[^/]+)/$', ImageListView.as_view(), name='tag'),

    url(r'^user/(?P<username>\w+)/albums/', AlbumListView.as_view(), name='user'),
    url(r'^user/(?P<username>\w+)/$', ImageListView.as_view(), name='user-images'),

    url(r'^upload/$', CreateImage.as_view(), name='upload'),
]

据我了解,我需要以某种方式删除url(r'^gallery/', include('imagestore.urls', namespace='imagestore')),,将profiles.urls部分添加到include('imagestore.urls'),但同时以某种方式保留namespace='imagestore'

我试过了:

urlpatterns = [
    url(r'^$', views.profiles_home, name='profiles_home'),
    url(r'', include('imagestore.urls', namespace='imagestore')),
]

Exception Type: NoReverseMatch
Exception Value: 'imagestore' is not a registered namespace

编辑: 当我尝试下面的两个解决方案并且它们不起作用时,我的猜测是问题是由于

中的参数<user_url>
url(r'^(?P<user_url>[\w.-]+)/', include('imagestore.urls', namespace='imagestore')),

imagestore无法处理,因为任何其他名称(例如r'^lalala/)都可以使用。是否有hide imagestore这个参数的public class CarriedItem : IItem { [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int CarriedItemId { get; set; } [Required] public int Size { get; set; } [Required] public string Name { get; set; } public string Description { get; set; } [Required] public bool GameOrPay { get; set; } [Required] public string Cost { get; set; } } 怎么样?

3 个答案:

答案 0 :(得分:0)

如果imagestore.urls中包含名称空间为imagestore的{​​{1}} profiles.urls,则创建一个嵌套的命名空间profiles

尝试在核心网址中加入profiles:imagestore,其基本网址与imagestore.urls相同。

profiles.urls

答案 1 :(得分:0)

您想将/gallery/替换为/<someusername>/吗?如果是这样,试试这个:

#core urls
urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='base.html'), name="home"),
    url(r'^(?P<user_url>[\w.-]+)/', include('profiles.urls', namespace='profiles_username')),
    url(r'^(?P<user_url>[\w.-]+)/', include('imagestore.urls', namespace='imagestore')),
]

像以前一样离开profiles.urls。这样,当请求URL时,Django将首先检查profiles.urls,如果路径只是一个用户名,则会有匹配,它会尝试获取此用户的页面。否则Django将检查下一个选项,看看是否有匹配,它将加载imagestore.urls

答案 2 :(得分:0)

解决方案是:

public class test
{
    public String fileText;

    public static void main(String[] args)
    {
     StartUpFile bob = new StartUpFile();

       System.out.println(bob.showFile());
       System.out.println(bob.temp());

       bob.change();
       System.out.println(bob.showFile());

    }
}

public class StartUpFile
{
    private String fileText;
    private String newText;
    /**
     * Constructor for objects of class StartUpFile
     */
    public StartUpFile()
    {
         newText = "\\\\"+"LocalPC";
         fileText = "Dir=C:"+"\\\\"+"Users"+"\\\\"+"admin"+"\\\\"+"AppData"+"\\\\"+"Local"+"\\\\"+"Temp";
    }
    /**
    * I want to ID the text bewtween "Dir=" and "Temp"
    */
    public String temp()
    {
      int startTemp = fileText.indexOf("Dir=") + 4;
      int EndTemp = fileText.indexOf("Temp");
      return fileText.substring(startTemp,EndTemp);
    }
    /**
    * show the string
    */ 
    public String showFile()
    {
        return fileText;
    }
    /**
     * swap the temp string for the newText
     */
    public void change()
    {
        fileText = fileText.replaceAll(temp(),newText);
    }
}

即。 # core urls urlpatterns = [ url(r'^$', TemplateView.as_view(template_name='base.html'), name="home"), url(r'^(?P<user_url>[\w.-]+)/', include('profiles.urls', namespace='profiles_username')), url(r'^[\w.-]+/', include('imagestore.urls', namespace='imagestore')), ] 已更改为r'^(?P<user_url>[\w.-]+)/',r'^[\w.-]+/',<user_url>发送一个无法处理的变量。