Django如何使用URL中的slug作为变量

时间:2017-09-04 07:24:09

标签: python django django-rest-framework slug

我想将URL中的slug用作我的API的变量。所以首先我需要把slu put放在urls.py中,看起来像这样。

url(r'^use/(?P<slug>[\w-]+)/$', view_function, name="api")

然后我需要在view.py中包含slug,看起来像这样:

def urlextractor(request, slug):
    if request.method == 'POST':
        serializer = Serializer(data=slug)
        # do something with slug

问题是我得到了代码404.你能给我一些我可以看一下的例子或参考文献吗?

2 个答案:

答案 0 :(得分:0)

在你的情况下:

public partial class FileDialog : Window
{
    private ObservableCollection<FileDetails> m_fileDialog;
    public FileDialog()
    {
        InitializeComponent();
        this.DataContext = FileUpload; 
    }  

    public FileUploadVM FileUpload
    {
        get
        {
            return new FileUploadVM();
        }
    }

    public ObservableCollection<FileDetails> DialogFiles
    {
        get
        {
            return m_fileDialog ?? FileUpload.DialogFiles;
        }
    }

    private void dataGrid1_Drop(object sender, DragEventArgs e)
    {
        string[] droppedFiles = null;
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            droppedFiles = e.Data.GetData(DataFormats.FileDrop, true) as string[];
        }
        if ((null == droppedFiles) || (!droppedFiles.Any()))
        {
            return;
        }
        foreach (var item in droppedFiles)
        {
            FileUploadVM.m_fileList.Add(item);
        }            
        FileUpload.GetFileWithIcon();
        FileUpload.DialogFiles = new ObservableCollection<FileDetails>(FileUploadVM.dgFiles);
        FileUploadVM.m_fileList.Clear();
    }
}
如果您未在自己的项目中添加url(r'^use/(?P<slug>[\w-]+)/$', view_function, name="api") ,则

您的网址应为"127.0.0.1:8000/use/variable" urls.py

api

答案 1 :(得分:0)

已删除评论的用户已回答了该问题。他说,为了让我的模块将URL作为变量,我只需要将urls.py更改为正则表达式:

  

url(r&#39; ^使用/(?P [A-Za-z] +)/ $&#39;,view_function,name =&#34; api&#34;)